1
0
mirror of https://github.com/django/django.git synced 2025-10-27 23:56:08 +00:00

[3.2.x] Fixed #32478 -- Included nested columns referenced by subqueries in GROUP BY on aggregations.

Regression in fb3f034f1c.

Refs #31094, #31150.

Thanks Igor Pejic for the report.

Backport of 277eea8fcc from master
This commit is contained in:
Simon Charette
2021-02-23 20:56:29 -05:00
committed by Mariusz Felisiak
parent 543171873f
commit 7a6ca01f4e
3 changed files with 27 additions and 4 deletions

View File

@@ -1311,6 +1311,21 @@ class AggregateTestCase(TestCase):
# self.assertSequenceEqual(books_qs, [book])
# self.assertEqual(ctx[0]['sql'].count('SELECT'), 2)
@skipUnlessDBFeature('supports_subqueries_in_group_by')
def test_aggregation_nested_subquery_outerref(self):
publisher_with_same_name = Publisher.objects.filter(
id__in=Subquery(
Publisher.objects.filter(
name=OuterRef(OuterRef('publisher__name')),
).values('id'),
),
).values(publisher_count=Count('id'))[:1]
books_breakdown = Book.objects.annotate(
publisher_count=Subquery(publisher_with_same_name),
authors_count=Count('authors'),
).values_list('publisher_count', flat=True)
self.assertSequenceEqual(books_breakdown, [1] * 6)
def test_aggregation_random_ordering(self):
"""Random() is not included in the GROUP BY when used for ordering."""
authors = Author.objects.annotate(contact_count=Count('book')).order_by('?')