[Django] #34005: In Django 3.2 the ORM looses Meta.ordering default order when .annotate is used

9 views
Skip to first unread message

Django

unread,
Sep 10, 2022, 6:33:49 PM9/10/22
to django-...@googlegroups.com
#34005: In Django 3.2 the ORM looses Meta.ordering default order when .annotate is
used
-------------------------------------+-------------------------------------
Reporter: pzdump | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 3.2
layer (models, ORM) | Keywords: orm Meta
Severity: Normal | Options.ordering annotate
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hi,
I've upgraded from 2.2 to 3.2 and now default ordering specified in the
Model does not work anymore if i annotate the QuerySet.
I suspect this is a ORM Bug, as the ordering / default order is not in the
query anymore since the update to 3.2.


Product class:
{{{
class Product(models.Model):
...
class Meta:
ordering = ["name"]
}}}


Default ordering is working nicely without annotate ... see ORDER BY
{{{
>>> print(Product.objects.all().select_related("category").query)
SELECT "memberapp_product"."id", [...] FROM "memberapp_product" INNER
JOIN "memberapp_productcategory" ON ("memberapp_product"."category_id" =
"memberapp_productcategory"."id") ORDER BY "memberapp_product"."name" ASC
}}}

but not anymore when annotate (with a special sum) is used - no ORDER BY
anymore in the query:
{{{
>>>
print(Product.objects.all().select_related("category").annotate(**kwargs).query)
SELECT "memberapp_product"."id", [...] LEFT OUTER JOIN
"memberapp_appointment" ON
("memberapp_appointmentattendee"."appointment_id" =
"memberapp_appointment"."id") INNER JOIN "memberapp_productcategory" ON
("memberapp_product"."category_id" = "memberapp_productcategory"."id")
GROUP BY "memberapp_product"."id", "memberapp_productcategory"."id"
}}}

Since it's been working before i would expect something in the ORM looses
the order by in complex queries using annotate.

Thanks for looking into this.

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

Django

unread,
Sep 10, 2022, 6:38:30 PM9/10/22
to django-...@googlegroups.com
#34005: In Django 3.2 the ORM looses Meta.ordering default order when .annotate is
used
-------------------------------------+-------------------------------------
Reporter: Pieter Zieschang | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm Meta | Triage Stage:
Options.ordering annotate | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Pieter Zieschang:

Old description:

New description:

Hi,
I've upgraded from 2.2 to 3.2.15 and now default ordering specified in the

--

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

Django

unread,
Sep 10, 2022, 7:08:28 PM9/10/22
to django-...@googlegroups.com
#34005: In Django 3.2 the ORM looses Meta.ordering default order when .annotate is
used
-------------------------------------+-------------------------------------
Reporter: Pieter Zieschang | Owner: nobody
Type: Bug | Status: closed

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

Keywords: orm Meta | Triage Stage:
Options.ordering annotate | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

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


Comment:

Model `Meta.ordering` affecting GROUP BY queries was
[https://docs.djangoproject.com/en/dev/releases/2.2/#model-meta-ordering-
will-no-longer-affect-group-by-queries deprecated in Django 2.2] and
removed in Django 3.1.

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

Reply all
Reply to author
Forward
0 new messages