summaryrefslogtreecommitdiff
path: root/django/db/models/sql/query.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2020-05-13 23:38:29 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-05-14 08:40:40 +0200
commitafceb2241ba84dfafe59de326cdc9c01963ddefb (patch)
treed39cc3ece50f4a5592a81d703c1cb184e5e01632 /django/db/models/sql/query.py
parent6e8a11e88cb11737a3230da45b9206bd3c7a2d98 (diff)
[3.0.x] Fixed #31566 -- Fixed aliases crash when chaining values()/values_list() after annotate() with aggregations and subqueries.
Subquery annotation references must be resolved if they are excluded from the GROUP BY clause by a following .values() call. Regression in fb3f034f1c63160c0ff13c609acd01c18be12f80. Thanks Makina Corpus for the report. Backport of 42c08ee46539ef44f8658ebb1cbefb408e0d03fe from master
Diffstat (limited to 'django/db/models/sql/query.py')
-rw-r--r--django/db/models/sql/query.py9
1 files changed, 9 insertions, 0 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 638161afc2..329a6e16c0 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -2141,6 +2141,15 @@ class Query(BaseExpression):
# SELECT clause which is about to be cleared.
self.set_group_by(allow_aliases=False)
self.clear_select_fields()
+ elif self.group_by:
+ # Resolve GROUP BY annotation references if they are not part of
+ # the selected fields anymore.
+ group_by = []
+ for expr in self.group_by:
+ if isinstance(expr, Ref) and expr.refs not in field_names:
+ expr = self.annotations[expr.refs]
+ group_by.append(expr)
+ self.group_by = tuple(group_by)
self.values_select = tuple(field_names)
self.add_fields(field_names, True)