Opened 3 weeks ago

Closed 33 hours ago

Last modified 33 hours ago

#36686 closed Cleanup/optimization (fixed)

Clarify the behavior difference between order_by() and Meta.ordering in GROUP BY queries

Reported by: Wouter Owned by: Varun Kasyap Pentamaraju
Component: Documentation Version: 5.2
Severity: Normal Keywords: aggregate, order_by, ordering
Cc: Varun Kasyap Pentamaraju Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Today I hunted an ordering bug. I have a model with ordering = ['name']

And in a view I use:
queryset.annotate(used_by=Count('somerelation')) which drops the order_by.

However the documentation for a aggregation states: https://docs.djangoproject.com/en/5.2/topics/db/aggregation/ that:
Django never removes ordering constraints that you have specified.

But then I saw the changelog for version. 2.2. Which explicitly states:

A model’s Meta.ordering affecting GROUP BY queries (such as .annotate().values()) is a common source of confusion. Such queries now issue a deprecation warning with the advice to add an order_by() to retain the current query. Meta.ordering will be ignored in such queries starting in Django 3.1.

Where do I find this information in the current documentation? I think this information is currently missing.

Change History (17)

comment:1 by Jacob Walls, 3 weeks ago

Triage Stage: UnreviewedAccepted

The current documentation only discusses the effect of performing a GROUP BY query after a queryset with .order_by(), but I agree that from this you cannot deduce the effect of doing the same on a model with Meta.ordering, which you might assume acts the same (especially given the "Django never..." wording you quote).

Clarifying that note would be welcome. Would you like to submit a PR?

comment:2 by Jacob Walls, 3 weeks ago

Keywords: aggregate order_by ordering added
Summary: Update documentation on 'never removes' ordering.Clarify the behavior difference between order_by() and Meta.ordering in GROUP BY queries

comment:3 by Jacob Walls, 3 weeks ago

I just happened to bump into this additional language:

Similarly, if you use a values() query to restrict the columns selected, the columns used in any order_by() (or default model ordering) will still be involved and may affect uniqueness of the results.

Might want to clarify that one also?

comment:4 by Jacob Walls, 3 weeks ago

Type: BugCleanup/optimization

comment:5 by Varun Kasyap Pentamaraju, 3 weeks ago

Cc: Varun Kasyap Pentamaraju added
Owner: set to Varun Kasyap Pentamaraju
Status: newassigned

I would like to work on this and raise PR, Thanks

comment:6 by Varun Kasyap Pentamaraju, 3 weeks ago

Has patch: set

comment:7 by Wouter, 2 weeks ago

The patches look okay to me.
What still is difficult to grasp is why order_by 'always' works if applied explicitly, but using ordering does not.

comment:8 by Jacob Walls, 2 weeks ago

Patch needs improvement: set

comment:9 by Varun Kasyap Pentamaraju, 2 weeks ago

Patch needs improvement: unset

1)Changed from "or" to "and"
2)Used admonition instead of note
3)Better English

comment:10 by Jacob Walls, 2 weeks ago

Patch needs improvement: set

comment:11 by Varun Kasyap Pentamaraju, 2 weeks ago

Patch needs improvement: unset

1) reversed order of values() and annotate()
2) reverted querysets.txt

Last edited 2 weeks ago by Varun Kasyap Pentamaraju (previous) (diff)

comment:12 by Jacob Walls, 2 weeks ago

Triage Stage: AcceptedReady for checkin

comment:13 by Jacob Walls, 35 hours ago

Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

comment:14 by Jacob Walls, 35 hours ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:15 by Jacob Walls <jacobtylerwalls@…>, 33 hours ago

Resolution: fixed
Status: assignedclosed

In 7e765a6:

Fixed #36686 -- Clarified Meta.ordering is ignored in GROUP BY queries.

comment:16 by Jacob Walls <jacobtylerwalls@…>, 33 hours ago

In 37b5dced:

[6.0.x] Fixed #36686 -- Clarified Meta.ordering is ignored in GROUP BY queries.

Backport of 7e765a68598b2b798e49bf1f4b431a7bcac085a4 from main.

comment:17 by Jacob Walls <jacobtylerwalls@…>, 33 hours ago

In edec3e59:

[5.2.x] Fixed #36686 -- Clarified Meta.ordering is ignored in GROUP BY queries.

Backport of 7e765a68598b2b798e49bf1f4b431a7bcac085a4 from main.

Note: See TracTickets for help on using tickets.
Back to Top