Opened 14 months ago

Closed 13 months ago

Last modified 13 months ago

#31990 closed Bug (fixed)

QuerySet.ordered property is incorrect for GROUP BY queries on models with Meta.ordering.

Reported by: Julien Dutriaux Owned by: Mariusz Felisiak
Component: Database layer (models, ORM) Version: 3.1
Severity: Release blocker Keywords:
Cc: Ramiro Morales Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Using the annotate function on a queryset doesn't keep the default ordering set in model's meta class.

A property should say whether the queryset will be ordered or not. I wanted to use the qs.ordered property for this but it seems to stay truthy, even if the resulting SQL query will not have an ORDER BY clause.

Example:

qs = Foo.objects.all()
​
# SQL => 'SELECT "foo_foo"."uuid", "foo_foo"."name" FROM "foo_foo" ORDER BY "foo_foo"."name" ASC'
​
qs.ordered # => True
qs.query.default_ordering # => True
​
############################################
​
qs2 = Foo.objects.annotate(Count("pk")).all()
​
# SQL => 'SELECT "foo_foo"."uuid", "foo_foo"."name", COUNT("foo_foo"."uuid") AS "pk__count" FROM "foo_foo" GROUP BY "foo_foo"."uuid"'
​
qs2.ordered # => True
qs2.query.default_ordering # => True

If it can help : I'm using PostgreSQL

Attachments (1)

test-31990.diff (631 bytes) - added by Mariusz Felisiak 14 months ago.
Regression test.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 14 months ago by Mariusz Felisiak

Resolution: worksforme
Status: newclosed
Summary: Queryset "ordered" property is misleading if "annotate" function is usedQuerySet.ordered property is incorrect after annotate().

Thanks for this report, however QuerySet.ordered works for me, see tests.

comment:2 in reply to:  1 Changed 14 months ago by Julien Dutriaux

Resolution: worksforme
Status: closednew

Replying to felixxm:

Thanks for this report, however QuerySet.ordered works for me, see tests.

Thanks for your answer but I've just checked again as it is in my example and ordered still returns True if an annotation is applied.
As said earlier, my model has a default ordering in the Meta class, the one in your unit test doesn't have.
If I modify the Annotation model to add a default ordering, the test doesn't pass anymore.
It can be the intended behavior but it just sounds misleading to me (having an ordered property sets to True while the results will not be ordered).

comment:3 Changed 14 months ago by Mariusz Felisiak

Cc: Ramiro Morales added
Severity: NormalRelease blocker
Summary: QuerySet.ordered property is incorrect after annotate().QuerySet.ordered property is incorrect for GROUP BY queries on models with Meta.ordering.
Triage Stage: UnreviewedAccepted

Thanks for clarification, Meta.ordering doesn't affect GROUP BY queries after 0ddb4ebf7bfcc4730c80a772dd146a49ef6895f6 (which was deprecated in 1b1f64ee5a78cc217fead52cbae23114502cf564). We should adjust QuerySet.ordered.

Changed 14 months ago by Mariusz Felisiak

Attachment: test-31990.diff added

Regression test.

comment:4 Changed 13 months ago by Mariusz Felisiak

Owner: changed from nobody to Mariusz Felisiak
Status: newassigned

comment:5 Changed 13 months ago by Mariusz Felisiak

Has patch: set

comment:6 Changed 13 months ago by GitHub <noreply@…>

Resolution: fixed
Status: assignedclosed

In e11d05e0:

Fixed #31990 -- Fixed QuerySet.ordered for GROUP BY queries on models with Meta.ordering.

Regression in 0ddb4ebf7bfcc4730c80a772dd146a49ef6895f6.

Thanks Julien Dutriaux for the report.

comment:7 Changed 13 months ago by Mariusz Felisiak <felisiak.mariusz@…>

In a3bb80dc:

[3.1.x] Fixed #31990 -- Fixed QuerySet.ordered for GROUP BY queries on models with Meta.ordering.

Regression in 0ddb4ebf7bfcc4730c80a772dd146a49ef6895f6.

Thanks Julien Dutriaux for the report.
Backport of e11d05e0b488a3ff2b3c9d8f2e1e50f471750d6e from master

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