#34372 closed Bug (fixed)
Field position reference for aggregate ends up in group-by clause
Reported by: | Jannis Vajen | Owned by: | Simon Charette |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 4.2 |
Severity: | Release blocker | Keywords: | orm aggregation |
Cc: | 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
Changeset 278881e37619278789942513916acafaa88d26f3 introduced a regression. Aggregate queries are rejected by the database due to the aggregated field being added to the GROUP BY clause.
It was difficult for me to pin down, especially because it looks like the error only occurs on the second evaluation of the query. The first query is executed just fine and doesn't contain the position reference to the aggregated field in the GROUP BY, only the second one. Below is a test to reproduce the behaviour:
-
tests/aggregation_regress/tests.py
diff --git a/tests/aggregation_regress/tests.py b/tests/aggregation_regress/tests.py index bfb3919b23..05122db956 100644
a b class AggregationTests(TestCase): 1321 1321 lambda b: (b.name, b.authorCount), 1322 1322 ) 1323 1323 1324 def test_quoting_aggregate_order_by_f(self): 1325 author = Author.objects.get(name="Peter Norvig") 1326 qs = ( 1327 author.book_set.all() 1328 .annotate(num=Count("authors")) 1329 .order_by(F("num").desc()) 1330 ) 1331 list(qs.iterator()) 1332 list(qs.iterator()) 1333 1324 1334 def test_stddev(self): 1325 1335 self.assertEqual( 1326 1336 Book.objects.aggregate(StdDev("pages")),
Change History (6)
comment:1 by , 21 months ago
Type: | Uncategorized → Bug |
---|
comment:2 by , 21 months ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 21 months ago
Has patch: | set |
---|
comment:4 by , 21 months ago
Summary: | Regression: field position reference for aggregate ends up in group-by clause → Field position reference for aggregate ends up in group-by clause |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Note:
See TracTickets
for help on using tickets.
Thanks a lot for the report and the test Jannis, pretty sure this due to a lack of
field.copy()
278881e37619278789942513916acafaa88d26f3 if this only happens on query re-evaluation.