Re: [Django] #14357: Prevent inappropriate order-based grouping on values+annotate queries

24 views
Skip to first unread message

Django

unread,
Aug 6, 2018, 4:44:21 PM8/6/18
to django-...@googlegroups.com
#14357: Prevent inappropriate order-based grouping on values+annotate queries
-------------------------------------+-------------------------------------
Reporter: Martin Chase | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* needs_better_patch: 1 => 0


Comment:

[https://github.com/django/django/pull/10005 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/14357#comment:17>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 13, 2018, 12:30:38 PM9/13/18
to django-...@googlegroups.com
#14357: Prevent inappropriate order-based grouping on values+annotate queries
-------------------------------------+-------------------------------------
Reporter: Martin Chase | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"1b1f64ee5a78cc217fead52cbae23114502cf564" 1b1f64ee]:
{{{
#!CommitTicketReference repository=""
revision="1b1f64ee5a78cc217fead52cbae23114502cf564"
Refs #14357 -- Deprecated Meta.ordering affecting GROUP BY queries.

Thanks Ramiro Morales for contributing to the patch.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/14357#comment:18>

Django

unread,
Sep 14, 2018, 3:13:48 AM9/14/18
to django-...@googlegroups.com
#14357: Prevent inappropriate order-based grouping on values+annotate queries
-------------------------------------+-------------------------------------
Reporter: Martin Chase | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* has_patch: 1 => 0


Comment:

I'm unchecking ''Has patch'', since the PR (deprecating `ordering` being
used for grouped queries was merged). What remains to close this ticket?

--
Ticket URL: <https://code.djangoproject.com/ticket/14357#comment:19>

Django

unread,
Sep 14, 2018, 8:43:45 AM9/14/18
to django-...@googlegroups.com
#14357: Prevent inappropriate order-based grouping on values+annotate queries
-------------------------------------+-------------------------------------
Reporter: Martin Chase | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution:
Keywords: 3.1 | Triage Stage:
| Someday/Maybe

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* keywords: => 3.1
* stage: Accepted => Someday/Maybe


Comment:

The behavior change will happen in Django 3.1 when the deprecation ends.

--
Ticket URL: <https://code.djangoproject.com/ticket/14357#comment:20>

Django

unread,
Sep 10, 2019, 6:19:57 AM9/10/19
to django-...@googlegroups.com
#14357: Prevent inappropriate order-based grouping on values+annotate queries
-------------------------------------+-------------------------------------
Reporter: Martin Chase | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution:
Keywords: 3.1 | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"0ddb4ebf7bfcc4730c80a772dd146a49ef6895f6" 0ddb4ebf]:
{{{
#!CommitTicketReference repository=""
revision="0ddb4ebf7bfcc4730c80a772dd146a49ef6895f6"
Refs #14357 -- Made Meta.ordering not affect GROUP BY queries.

Per deprecation timeline.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/14357#comment:21>

Django

unread,
May 20, 2020, 3:38:40 AM5/20/20
to django-...@googlegroups.com
#14357: Prevent inappropriate order-based grouping on values+annotate queries
-------------------------------------+-------------------------------------
Reporter: Martin Chase | Owner: (none)
Type: Bug | Status: closed

Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: 3.1 | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* resolution: => fixed


--
Ticket URL: <https://code.djangoproject.com/ticket/14357#comment:22>

Django

unread,
Mar 13, 2021, 1:29:32 PM3/13/21
to django-...@googlegroups.com
#14357: Prevent inappropriate order-based grouping on values+annotate queries
-------------------------------------+-------------------------------------
Reporter: Martin Chase | Owner: (none)
Type: Bug | Status: new

Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution:
Keywords: 3.1 | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by nE0sIghT):

* status: closed => new
* resolution: fixed =>


Comment:

I beleave this was not properly fixed in commit 0ddb4ebf.
While commit 0ddb4ebf removes `ORDER BY` when Meta.ordering is used it
still does populates `GROUP BY` with Meta.ordering fields thus leads to
wrong aggregation.

Look to get_group_by() at compiler.py [1] - it populates `GROUP BY` using
previously obtained order_by with `Meta.ordering` fields [2][3].
For what I see we must filter off `Meta.ordering` fields from order_by in
get_group_by() function.

[1]
https://github.com/django/django/blob/3.1.7/django/db/models/sql/compiler.py#L128
[2]
https://github.com/django/django/blob/3.1.7/django/db/models/sql/compiler.py#L56
[3]
https://github.com/django/django/blob/3.1.7/django/db/models/sql/compiler.py#L286

--
Ticket URL: <https://code.djangoproject.com/ticket/14357#comment:23>

Django

unread,
Mar 13, 2021, 1:58:56 PM3/13/21
to django-...@googlegroups.com
#14357: Prevent inappropriate order-based grouping on values+annotate queries
-------------------------------------+-------------------------------------
Reporter: Martin Chase | Owner: (none)
Type: Bug | Status: closed

Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: 3.1 | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => fixed


Comment:

Please don't reopen closed tickets. You can create a new ticket if you can
provide a sample project or test and prove that `Meta.ordering` is still
included.

--
Ticket URL: <https://code.djangoproject.com/ticket/14357#comment:24>

Django

unread,
Jun 8, 2021, 10:39:43 AM6/8/21
to django-...@googlegroups.com
#14357: Prevent inappropriate order-based grouping on values+annotate queries
-------------------------------------+-------------------------------------
Reporter: Martin Chase | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: 3.1 | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by GitHub <noreply@…>):

In [changeset:"fcd44b889f36c4be87910745614a0a4c88d7a3d8" fcd44b88]:
{{{
#!CommitTicketReference repository=""
revision="fcd44b889f36c4be87910745614a0a4c88d7a3d8"
Refs #14357 -- Updated docs about interaction between aggregations and
QuerySet.order_by().

Obsolete since 0ddb4ebf7bfcc4730c80a772dd146a49ef6895f6.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/14357#comment:25>

Django

unread,
Jun 8, 2021, 10:40:13 AM6/8/21
to django-...@googlegroups.com
#14357: Prevent inappropriate order-based grouping on values+annotate queries
-------------------------------------+-------------------------------------
Reporter: Martin Chase | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: 3.1 | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"9b096063c13085d06236a72dcc46915f4c4ec28a" 9b096063]:
{{{
#!CommitTicketReference repository=""
revision="9b096063c13085d06236a72dcc46915f4c4ec28a"
[3.2.x] Refs #14357 -- Updated docs about interaction between aggregations
and QuerySet.order_by().

Obsolete since 0ddb4ebf7bfcc4730c80a772dd146a49ef6895f6.
Backport of fcd44b889f36c4be87910745614a0a4c88d7a3d8 from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/14357#comment:26>

Django

unread,
Jan 10, 2022, 2:10:46 PM1/10/22
to django-...@googlegroups.com
#14357: Prevent inappropriate order-based grouping on values+annotate queries
-------------------------------------+-------------------------------------
Reporter: Martin Chase | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: 3.1 | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Adam Sołtysik):

For anyone surprised, just like me, that `Meta.ordering` still breaks
aggregation queries in Django 3.1 and 3.2, despite what the documentation
says [https://docs.djangoproject.com/en/4.0/releases/3.1/#features-
removed-in-3-1 here] and
[https://docs.djangoproject.com/en/4.0/releases/2.2/#model-meta-ordering-
will-no-longer-affect-group-by-queries here], it seems that this was
finally fixed in #32546 in Django 4.0. Possibly the documentation should
be updated to reflect the actual version when the change was implemented,
as there seems to be nothing about it in the
[https://docs.djangoproject.com/en/4.0/releases/4.0/ release notes for
4.0]. Or maybe the change should be backported to reflect the
documentation.

--
Ticket URL: <https://code.djangoproject.com/ticket/14357#comment:27>

Reply all
Reply to author
Forward
0 new messages