mirror of
https://github.com/django/django.git
synced 2024-12-26 19:16:11 +00:00
[5.0.x] Fixed #34798 -- Fixed QuerySet.aggregate() crash when referencing expressions containing subqueries.
Regression in59bea9efd2
, complementse5c844d6f2
. Refs #28477, #34551. Thanks Haldun Komsuoglu for the report. Backport of3b4a571275
from main
This commit is contained in:
parent
12a19321b4
commit
4ccca9eedc
@ -256,6 +256,13 @@ class BaseExpression:
|
||||
for expr in self.get_source_expressions()
|
||||
)
|
||||
|
||||
@cached_property
|
||||
def contains_subquery(self):
|
||||
return any(
|
||||
expr and (getattr(expr, "subquery", False) or expr.contains_subquery)
|
||||
for expr in self.get_source_expressions()
|
||||
)
|
||||
|
||||
def resolve_expression(
|
||||
self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False
|
||||
):
|
||||
|
@ -452,7 +452,7 @@ class Query(BaseExpression):
|
||||
# members of `aggregates` to resolve against each others.
|
||||
self.append_annotation_mask([alias])
|
||||
refs_subquery |= any(
|
||||
getattr(self.annotations[ref], "subquery", False)
|
||||
getattr(self.annotations[ref], "contains_subquery", False)
|
||||
for ref in aggregate.get_refs()
|
||||
)
|
||||
refs_window |= any(
|
||||
|
@ -9,4 +9,6 @@ Django 4.2.7 fixes several bugs in 4.2.6.
|
||||
Bugfixes
|
||||
========
|
||||
|
||||
...
|
||||
* Fixed a regression in Django 4.2 that caused a crash of
|
||||
``QuerySet.aggregate()`` with aggregates referencing expressions containing
|
||||
subqueries (:ticket:`34798`).
|
||||
|
@ -2260,6 +2260,27 @@ class AggregateAnnotationPruningTests(TestCase):
|
||||
self.assertEqual(sql.count("select"), 3, "Subquery wrapping required")
|
||||
self.assertEqual(aggregate, {"sum_total_books": 3})
|
||||
|
||||
def test_referenced_composed_subquery_requires_wrapping(self):
|
||||
total_books_qs = (
|
||||
Author.book_set.through.objects.values("author")
|
||||
.filter(author=OuterRef("pk"))
|
||||
.annotate(total=Count("book"))
|
||||
)
|
||||
with self.assertNumQueries(1) as ctx:
|
||||
aggregate = (
|
||||
Author.objects.annotate(
|
||||
total_books=Subquery(total_books_qs.values("total")),
|
||||
total_books_ref=F("total_books") / 1,
|
||||
)
|
||||
.values("pk", "total_books_ref")
|
||||
.aggregate(
|
||||
sum_total_books=Sum("total_books_ref"),
|
||||
)
|
||||
)
|
||||
sql = ctx.captured_queries[0]["sql"].lower()
|
||||
self.assertEqual(sql.count("select"), 3, "Subquery wrapping required")
|
||||
self.assertEqual(aggregate, {"sum_total_books": 3})
|
||||
|
||||
@skipUnlessDBFeature("supports_over_clause")
|
||||
def test_referenced_window_requires_wrapping(self):
|
||||
total_books_qs = Book.objects.annotate(
|
||||
|
Loading…
Reference in New Issue
Block a user