Skip to content

Commit

Permalink
[4.2.x] Fixed #34748 -- Fixed queryset crash when grouping by a refer…
Browse files Browse the repository at this point in the history
…ence in a subquery.

Regression in dd68af6.

Thanks Toan Vuong for the report.

Backport of 4087367 from main
  • Loading branch information
charettes authored and felixxm committed Jul 30, 2023
1 parent a52a2b6 commit 739da73
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion django/db/models/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,9 @@ def get_refs(self):
return {self.refs}

def relabeled_clone(self, relabels):
return self
clone = self.copy()
clone.source = self.source.relabeled_clone(relabels)
return clone

def as_sql(self, compiler, connection):
return connection.ops.quote_name(self.refs), []
Expand Down
3 changes: 3 additions & 0 deletions docs/releases/4.2.4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ Bugfixes
* Fixed a regression in Django 4.2 that caused a crash of
``QuerySet.aggregate()`` with aggregates referencing window functions
(:ticket:`34717`).

* Fixed a regression in Django 4.2 that caused a crash when grouping by a
reference in a subquery (:ticket:`34748`).
10 changes: 10 additions & 0 deletions tests/aggregation/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2116,6 +2116,16 @@ def test_multiple_aggregate_references(self):
},
)

def test_group_by_reference_subquery(self):
author_qs = (
Author.objects.annotate(publisher_id=F("book__publisher"))
.values("publisher_id")
.annotate(cnt=Count("*"))
.values("publisher_id")
)
qs = Publisher.objects.filter(pk__in=author_qs)
self.assertCountEqual(qs, [self.p1, self.p2, self.p3, self.p4])


class AggregateAnnotationPruningTests(TestCase):
@classmethod
Expand Down

0 comments on commit 739da73

Please sign in to comment.