From e5c844d6f2a4ac6ae674d741b5f1fa2a688cedf4 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Sun, 21 May 2023 23:57:49 -0400 Subject: Fixed #34551 -- Fixed QuerySet.aggregate() crash when referencing subqueries. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Regression in 59bea9efd2768102fc9d3aedda469502c218e9b7. Refs #28477. Thanks Denis Roldán and Mariusz for the test. --- tests/aggregation/tests.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'tests/aggregation') diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py index 5135458668..366b8434e5 100644 --- a/tests/aggregation/tests.py +++ b/tests/aggregation/tests.py @@ -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}) -- cgit v1.3