diff options
| author | Simon Charette <charette.s@gmail.com> | 2023-10-15 21:59:15 -0400 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-10-16 05:37:30 +0200 |
| commit | 3b4a571275d967512866012955eb0b3ae486d63c (patch) | |
| tree | 36a2db000304ab32b3a287d11449284c30245e91 /tests | |
| parent | 73b2c63127297e214d3bfdc8d9a96837691fc2a0 (diff) | |
Fixed #34798 -- Fixed QuerySet.aggregate() crash when referencing expressions containing subqueries.
Regression in 59bea9efd2768102fc9d3aedda469502c218e9b7,
complements e5c844d6f2a4ac6ae674d741b5f1fa2a688cedf4.
Refs #28477, #34551.
Thanks Haldun Komsuoglu for the report.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/aggregation/tests.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py index ad00afdcc1..8d8e46e312 100644 --- a/tests/aggregation/tests.py +++ b/tests/aggregation/tests.py @@ -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( |
