Fixed #32152 -- Fixed grouping by subquery aliases.

Regression in 42c08ee465.

Thanks Simon Charette for the review.
This commit is contained in:
Christian Klus 2020-10-27 13:13:10 -05:00 committed by Mariusz Felisiak
parent 9ca22c7733
commit 4ac2d4fa42
4 changed files with 34 additions and 3 deletions

View file

@ -2210,8 +2210,10 @@ class Query(BaseExpression):
field_names.append(f)
self.set_extra_mask(extra_names)
self.set_annotation_mask(annotation_names)
selected = frozenset(field_names + extra_names + annotation_names)
else:
field_names = [f.attname for f in self.model._meta.concrete_fields]
selected = frozenset(field_names)
# Selected annotations must be known before setting the GROUP BY
# clause.
if self.group_by is True:
@ -2225,7 +2227,7 @@ class Query(BaseExpression):
# the selected fields anymore.
group_by = []
for expr in self.group_by:
if isinstance(expr, Ref) and expr.refs not in field_names:
if isinstance(expr, Ref) and expr.refs not in selected:
expr = self.annotations[expr.refs]
group_by.append(expr)
self.group_by = tuple(group_by)