mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
Fixed #34798 -- Fixed QuerySet.aggregate() crash when referencing expressions containing subqueries.
Regression in59bea9efd2, complementse5c844d6f2. Refs #28477, #34551. Thanks Haldun Komsuoglu for the report.
This commit is contained in:
committed by
Mariusz Felisiak
parent
73b2c63127
commit
3b4a571275
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user