diff options
| author | Simon Charette <charette.s@gmail.com> | 2023-07-28 07:45:23 -0400 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-07-30 07:51:52 +0200 |
| commit | 739da731645f394ccc56f64a5d7625f7aafd820b (patch) | |
| tree | 34ec1ce89620a8839bc9b2a36b0f116eba250627 | |
| parent | a52a2b6678ea2e35d86d97735d936e9b7bf73474 (diff) | |
[4.2.x] Fixed #34748 -- Fixed queryset crash when grouping by a reference in a subquery.
Regression in dd68af62b2b27ece50d434f6a351877212e15c3f.
Thanks Toan Vuong for the report.
Backport of 4087367ba869be9cf305dac39a8887d4aa4041d2 from main
| -rw-r--r-- | django/db/models/expressions.py | 4 | ||||
| -rw-r--r-- | docs/releases/4.2.4.txt | 3 | ||||
| -rw-r--r-- | tests/aggregation/tests.py | 10 |
3 files changed, 16 insertions, 1 deletions
diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index b3fee9052d..c6443892e2 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -1181,7 +1181,9 @@ class Ref(Expression): 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), [] diff --git a/docs/releases/4.2.4.txt b/docs/releases/4.2.4.txt index 3921dd9b3e..a39818e3b8 100644 --- a/docs/releases/4.2.4.txt +++ b/docs/releases/4.2.4.txt @@ -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`). diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py index db69246952..9f2a7c8841 100644 --- a/tests/aggregation/tests.py +++ b/tests/aggregation/tests.py @@ -2116,6 +2116,16 @@ class AggregateTestCase(TestCase): }, ) + 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 |
