diff options
| author | Simon Charette <charette.s@gmail.com> | 2023-11-17 19:42:44 -0500 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-11-18 16:53:24 +0100 |
| commit | acf4cee95144c55a12492cdd71fa795d7accfe26 (patch) | |
| tree | c4edecd96e42fa44c77ee6df2560a5641a4036e1 | |
| parent | 47f9b8dca16b1fdc054b338d81eb080eabad0edf (diff) | |
[4.2.x] Fixed #34975 -- Fixed crash of conditional aggregate() over aggregations.
Adjustments made to solve_lookup_type to defer the resolving of
references for summarized aggregates failed to account for similar
requirements for lookup values which can also reference annotations
through Aggregate.filter.
Regression in b181cae2e3697b2e53b5b67ac67e59f3b05a6f0d.
Refs #25307.
Thanks Sergey Nesterenko for the report.
Backport of 7530cf3900ab98104edcde69e8a2a415e82b345a from main
| -rw-r--r-- | django/db/models/sql/query.py | 5 | ||||
| -rw-r--r-- | docs/releases/4.2.8.txt | 4 | ||||
| -rw-r--r-- | tests/aggregation/tests.py | 6 |
3 files changed, 13 insertions, 2 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 0709c4a45f..f98c6c668b 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -1199,12 +1199,13 @@ class Query(BaseExpression): sql = "(%s)" % sql return sql, params - def resolve_lookup_value(self, value, can_reuse, allow_joins): + def resolve_lookup_value(self, value, can_reuse, allow_joins, summarize=False): if hasattr(value, "resolve_expression"): value = value.resolve_expression( self, reuse=can_reuse, allow_joins=allow_joins, + summarize=summarize, ) elif isinstance(value, (list, tuple)): # The items of the iterable may be expressions and therefore need @@ -1432,7 +1433,7 @@ class Query(BaseExpression): raise FieldError("Joined field references are not permitted in this query") pre_joins = self.alias_refcount.copy() - value = self.resolve_lookup_value(value, can_reuse, allow_joins) + value = self.resolve_lookup_value(value, can_reuse, allow_joins, summarize) used_joins = { k for k, v in self.alias_refcount.items() if v > pre_joins.get(k, 0) } diff --git a/docs/releases/4.2.8.txt b/docs/releases/4.2.8.txt index 5804589408..2229324362 100644 --- a/docs/releases/4.2.8.txt +++ b/docs/releases/4.2.8.txt @@ -11,3 +11,7 @@ Bugfixes * Fixed a regression in Django 4.2 that caused :option:`makemigrations --check` to stop displaying pending migrations (:ticket:`34457`). + +* Fixed a regression in Django 4.2 that caused a crash of + ``QuerySet.aggregate()`` with aggregates referencing other aggregates or + window functions through conditional expressions (:ticket:`34975`). diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py index 8d8e46e312..48266d9774 100644 --- a/tests/aggregation/tests.py +++ b/tests/aggregation/tests.py @@ -2300,3 +2300,9 @@ class AggregateAnnotationPruningTests(TestCase): aggregate, {"sum_avg_publisher_pages": 1100.0, "books_count": 2}, ) + + def test_aggregate_reference_lookup_rhs(self): + aggregates = Author.objects.annotate( + max_book_author=Max("book__authors"), + ).aggregate(count=Count("id", filter=Q(id=F("max_book_author")))) + self.assertEqual(aggregates, {"count": 1}) |
