Opened 4 years ago

Closed 4 years ago

Last modified 4 years 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 4 years ago.
Regression test.

Download all attachments as: .zip

Change History (8)

comment:1 by Mariusz Felisiak, 4 years ago

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.

in reply to:  1 comment:2 by Julien Dutriaux, 4 years ago

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 by Mariusz Felisiak, 4 years ago

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.

by Mariusz Felisiak, 4 years ago

Attachment: test-31990.diff added

Regression test.

comment:4 by Mariusz Felisiak, 4 years ago

Owner: changed from nobody to Mariusz Felisiak
Status: newassigned

comment:5 by Mariusz Felisiak, 4 years ago

Has patch: set

comment:6 by GitHub <noreply@…>, 4 years ago

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 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

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