diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index cc1d4b1d382..7429939cf9a 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -527,7 +527,9 @@ class Query(BaseExpression): if not q.distinct: if q.group_by is True: q.add_fields((f.attname for f in self.model._meta.concrete_fields), False) - q.set_group_by() + # Disable GROUP BY aliases to avoid orphaning references to the + # SELECT clause which is about to be cleared. + q.set_group_by(allow_aliases=False) q.clear_select_clause() q.clear_ordering(True) q.set_limits(high=1) @@ -1920,7 +1922,7 @@ class Query(BaseExpression): if force_empty: self.default_ordering = False - def set_group_by(self): + def set_group_by(self, allow_aliases=True): """ Expand the GROUP BY clause required by the query. @@ -1943,6 +1945,8 @@ class Query(BaseExpression): warnings.warn(msg, category=RemovedInDjango40Warning) group_by_cols = annotation.get_group_by_cols() else: + if not allow_aliases: + alias = None group_by_cols = annotation.get_group_by_cols(alias=alias) group_by.extend(group_by_cols) self.group_by = tuple(group_by) diff --git a/docs/releases/3.0.2.txt b/docs/releases/3.0.2.txt index 53b066838a0..ccc82b18aed 100644 --- a/docs/releases/3.0.2.txt +++ b/docs/releases/3.0.2.txt @@ -11,3 +11,6 @@ Bugfixes * Fixed a regression in Django 3.0 that didn't include columns referenced by a ``Subquery()`` in the ``GROUP BY`` clause (:ticket:`31094`). + +* Fixed a regression in Django 3.0 where ``QuerySet.exists()`` crashed if a + queryset contained an aggregation over a ``Subquery()`` (:ticket:`31109`). diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py index 5ba2e180e05..efc0a72c866 100644 --- a/tests/aggregation/tests.py +++ b/tests/aggregation/tests.py @@ -1139,6 +1139,16 @@ class AggregateTestCase(TestCase): list(publisher_qs) self.assertEqual(ctx[0]['sql'].count('SELECT'), 2) + def test_aggregation_subquery_annotation_exists(self): + latest_book_pubdate_qs = Book.objects.filter( + publisher=OuterRef('pk') + ).order_by('-pubdate').values('pubdate')[:1] + publisher_qs = Publisher.objects.annotate( + latest_book_pubdate=Subquery(latest_book_pubdate_qs), + count=Count('book'), + ) + self.assertTrue(publisher_qs.exists()) + @skipUnlessDBFeature('supports_subqueries_in_group_by') def test_group_by_subquery_annotation(self): """