Skip to content

Commit 4ac2d4f

Browse files
kluchrjfelixxm
authored andcommitted
Fixed #32152 -- Fixed grouping by subquery aliases.
Regression in 42c08ee. Thanks Simon Charette for the review.
1 parent 9ca22c7 commit 4ac2d4f

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

django/db/models/sql/query.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2210,8 +2210,10 @@ def set_values(self, fields):
22102210
field_names.append(f)
22112211
self.set_extra_mask(extra_names)
22122212
self.set_annotation_mask(annotation_names)
2213+
selected = frozenset(field_names + extra_names + annotation_names)
22132214
else:
22142215
field_names = [f.attname for f in self.model._meta.concrete_fields]
2216+
selected = frozenset(field_names)
22152217
# Selected annotations must be known before setting the GROUP BY
22162218
# clause.
22172219
if self.group_by is True:
@@ -2225,7 +2227,7 @@ def set_values(self, fields):
22252227
# the selected fields anymore.
22262228
group_by = []
22272229
for expr in self.group_by:
2228-
if isinstance(expr, Ref) and expr.refs not in field_names:
2230+
if isinstance(expr, Ref) and expr.refs not in selected:
22292231
expr = self.annotations[expr.refs]
22302232
group_by.append(expr)
22312233
self.group_by = tuple(group_by)

docs/releases/3.0.11.txt

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@ Django 3.0.11 release notes
44

55
*Expected November 2, 2020*
66

7-
Django 3.0.11 adds compatibility with Python 3.9.
7+
Django 3.0.11 fixes a regression in 3.0.7 and adds compatibility with Python
8+
3.9.
9+
10+
Bugfixes
11+
========
12+
13+
* Fixed a regression in Django 3.0.7 that didn't use ``Subquery()`` aliases in
14+
the ``GROUP BY`` clause (:ticket:`32152`).

docs/releases/3.1.3.txt

+3
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,6 @@ Bugfixes
5757
* Fixed a regression in Django 3.1 that caused incorrect textarea layout on
5858
medium-sized screens in the admin change form view with the sidebar open
5959
(:ticket:`32127`).
60+
61+
* Fixed a regression in Django 3.0.7 that didn't use ``Subquery()`` aliases in
62+
the ``GROUP BY`` clause (:ticket:`32152`).

tests/annotations/tests.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Subquery, Sum, Value, When,
1010
)
1111
from django.db.models.expressions import RawSQL
12-
from django.db.models.functions import Coalesce, Length, Lower
12+
from django.db.models.functions import Coalesce, ExtractYear, Length, Lower
1313
from django.test import TestCase, skipUnlessDBFeature
1414

1515
from .models import (
@@ -658,6 +658,25 @@ def test_annotation_exists_aggregate_values_chaining(self):
658658
datetime.date(2008, 11, 3),
659659
])
660660

661+
@skipUnlessDBFeature('supports_subqueries_in_group_by')
662+
def test_annotation_subquery_and_aggregate_values_chaining(self):
663+
qs = Book.objects.annotate(
664+
pub_year=ExtractYear('pubdate')
665+
).values('pub_year').annotate(
666+
top_rating=Subquery(
667+
Book.objects.filter(
668+
pubdate__year=OuterRef('pub_year')
669+
).order_by('-rating').values('rating')[:1]
670+
),
671+
total_pages=Sum('pages'),
672+
).values('pub_year', 'total_pages', 'top_rating')
673+
self.assertCountEqual(qs, [
674+
{'pub_year': 1991, 'top_rating': 5.0, 'total_pages': 946},
675+
{'pub_year': 1995, 'top_rating': 4.0, 'total_pages': 1132},
676+
{'pub_year': 2007, 'top_rating': 4.5, 'total_pages': 447},
677+
{'pub_year': 2008, 'top_rating': 4.0, 'total_pages': 1178},
678+
])
679+
661680
def test_annotation_aggregate_with_m2o(self):
662681
if connection.vendor == 'mysql' and 'ONLY_FULL_GROUP_BY' in connection.sql_mode:
663682
self.skipTest(

0 commit comments

Comments
 (0)