[Django] #29539: Cannot use Aggregation function in Model.Meta.ordering

22 views
Skip to first unread message

Django

unread,
Jul 1, 2018, 2:36:59 PM7/1/18
to django-...@googlegroups.com
#29539: Cannot use Aggregation function in Model.Meta.ordering
-------------------------------------+-------------------------------------
Reporter: wilhelmhb | Owner: nobody
Type: Bug | Status: new
Component: | Version: 2.0
Uncategorized | Keywords: ordering, query
Severity: Normal | expression, aggregation function
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Modifying the example from
[https://docs.djangoproject.com/en/2.0/topics/db/queries/#] to add
ordering:
{{{
from django.db import models

class Author(models.Model):
name = models.CharField(max_length=200)

class Entry(models.Model):
headline = models.CharField(max_length=255)
authors = models.ManyToManyField(Author)

def __str__(self):
return self.headline

class Meta:
ordering = [
models.Min('authors__id').asc(),
models.Max('authors__id').asc()
]
}}}

The query to get entries will fail with a {{{ProgrammingError}}}: the SQL
query doesn't contain the {{{GROUP BY}}} clause.
As aggregation functions are QueryExpression objects, and the ordering
field handles QueryExpressions since Django 2.0, it should work.

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

Django

unread,
Jul 1, 2018, 2:37:14 PM7/1/18
to django-...@googlegroups.com
#29539: Cannot use Aggregation function in Model.Meta.ordering
-------------------------------------+-------------------------------------
Reporter: wilhelmhb | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 2.0
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage:
expression, aggregation function | Unreviewed
Has patch: 0 | Needs documentation: 0

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

* cc: wilhelmhb (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/29539#comment:1>

Django

unread,
Jul 3, 2018, 10:01:19 AM7/3/18
to django-...@googlegroups.com
#29539: Cannot use Aggregation function in Model.Meta.ordering
-------------------------------------+-------------------------------------
Reporter: wilhelmhb | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage: Accepted
expression, aggregation function |
Has patch: 0 | Needs documentation: 0

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

* component: Uncategorized => Database layer (models, ORM)
* stage: Unreviewed => Accepted


Comment:

I'm not sure how this should be solved. The problem is that adding that
`order_by()` clause to a model without `Meta.ordering` doesn't work:
{{{
>>> Entry.objects.order_by(models.Min('authors__id').asc(),
models.Max('authors__id').asc()))
...
django.core.exceptions.FieldError: Using an aggregate in order_by()
without also including it in annotate() is not allowed:
OrderBy(Min(F(authors__id)), descending=False)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29539#comment:2>

Django

unread,
Mar 22, 2019, 3:49:31 PM3/22/19
to django-...@googlegroups.com
#29539: Cannot use Aggregation function in Model.Meta.ordering
-------------------------------------+-------------------------------------
Reporter: wilhelmhb | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage: Accepted
expression, aggregation function |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Can Sarıgöl):

* cc: Can Sarıgöl (added)
* has_patch: 0 => 1


Comment:

I tried to fix this. Could you review?
[https://github.com/django/django/pull/11111 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/29539#comment:3>

Django

unread,
Mar 23, 2019, 10:56:30 AM3/23/19
to django-...@googlegroups.com
#29539: Cannot use Aggregation function in Model.Meta.ordering
-------------------------------------+-------------------------------------
Reporter: wilhelmhb | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage: Accepted
expression, aggregation function |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* needs_better_patch: 0 => 1


Comment:

Removing the deprecation from #14357 isn't appropriate.

--
Ticket URL: <https://code.djangoproject.com/ticket/29539#comment:4>

Django

unread,
Mar 23, 2019, 12:32:34 PM3/23/19
to django-...@googlegroups.com
#29539: Cannot use Aggregation function in Model.Meta.ordering
-------------------------------------+-------------------------------------
Reporter: wilhelmhb | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage: Accepted
expression, aggregation function |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Can Sarıgöl):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/29539#comment:5>

Django

unread,
Mar 23, 2019, 6:45:28 PM3/23/19
to django-...@googlegroups.com
#29539: Cannot use Aggregation function in Model.Meta.ordering
-------------------------------------+-------------------------------------
Reporter: wilhelmhb | Owner: nobody
Type: Bug | Status: closed

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

Keywords: ordering, query | Triage Stage: Accepted
expression, aggregation function |
Has patch: 1 | Needs documentation: 0

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

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


Comment:

From Simon on the pull request:
I think this feature would be hard to get right for the rare case where
it's useful. It also go against our stance on trying to disconnect
Meta.ordering from aggregation (#14357) as this PR demonstrates. I'd be in
favour of won't fixing the ticket as well for these reasons. It feels
incoherent with the ongoing deprecation motivated by observed user
confusion over the years from the interactions between these two features.

--
Ticket URL: <https://code.djangoproject.com/ticket/29539#comment:6>

Reply all
Reply to author
Forward
0 new messages