1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #34551 -- Fixed QuerySet.aggregate() crash when referencing subqueries.

Regression in 59bea9efd2.

Refs #28477.

Thanks Denis Roldán and Mariusz for the test.
This commit is contained in:
Simon Charette
2023-05-21 23:57:49 -04:00
committed by Mariusz Felisiak
parent 2ee01747c3
commit e5c844d6f2
4 changed files with 31 additions and 0 deletions

View File

@@ -2187,3 +2187,23 @@ class AggregateAnnotationPruningTests(TestCase):
mod_count=Count("*")
)
self.assertEqual(queryset.count(), 1)
def test_referenced_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"))
)
.values("pk", "total_books")
.aggregate(
sum_total_books=Sum("total_books"),
)
)
sql = ctx.captured_queries[0]["sql"].lower()
self.assertEqual(sql.count("select"), 3, "Subquery wrapping required")
self.assertEqual(aggregate, {"sum_total_books": 3})