mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Refs #30188 -- Prevented double annotation of subquery when aggregated over.
Thanks Can Sarıgöl for the suggested trimming approach.
This commit is contained in:
parent
bdc07f176e
commit
d1e9c25162
2 changed files with 23 additions and 15 deletions
|
@ -553,16 +553,20 @@ class BasicExpressionsTests(TestCase):
|
|||
|
||||
@skipUnlessDBFeature('supports_subqueries_in_group_by')
|
||||
def test_aggregate_subquery_annotation(self):
|
||||
aggregate = Company.objects.annotate(
|
||||
ceo_salary=Subquery(
|
||||
Employee.objects.filter(
|
||||
id=OuterRef('ceo_id'),
|
||||
).values('salary')
|
||||
),
|
||||
).aggregate(
|
||||
ceo_salary_gt_20=Count('pk', filter=Q(ceo_salary__gt=20)),
|
||||
)
|
||||
with self.assertNumQueries(1) as ctx:
|
||||
aggregate = Company.objects.annotate(
|
||||
ceo_salary=Subquery(
|
||||
Employee.objects.filter(
|
||||
id=OuterRef('ceo_id'),
|
||||
).values('salary')
|
||||
),
|
||||
).aggregate(
|
||||
ceo_salary_gt_20=Count('pk', filter=Q(ceo_salary__gt=20)),
|
||||
)
|
||||
self.assertEqual(aggregate, {'ceo_salary_gt_20': 1})
|
||||
# Aggregation over a subquery annotation doesn't annotate the subquery
|
||||
# twice in the inner query.
|
||||
self.assertLessEqual(ctx.captured_queries[0]['sql'].count('SELECT'), 4,)
|
||||
|
||||
def test_explicit_output_field(self):
|
||||
class FuncA(Func):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue