#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)
Change History (8)
follow-up: 2 comment:1 by , 4 years ago
Resolution: | → worksforme |
---|---|
Status: | new → closed |
Summary: | Queryset "ordered" property is misleading if "annotate" function is used → QuerySet.ordered property is incorrect after annotate(). |
comment:2 by , 4 years ago
Resolution: | worksforme |
---|---|
Status: | closed → new |
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 by , 4 years ago
Cc: | added |
---|---|
Severity: | Normal → Release 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: | Unreviewed → Accepted |
Thanks for clarification, Meta.ordering
doesn't affect GROUP BY
queries after 0ddb4ebf7bfcc4730c80a772dd146a49ef6895f6 (which was deprecated in 1b1f64ee5a78cc217fead52cbae23114502cf564). We should adjust QuerySet.ordered
.
comment:4 by , 4 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Thanks for this report, however
QuerySet.ordered
works for me, see tests.