Fixed #31880 -- Made QuerySet.aggregate() raise FieldError when aggregating over aggregation aliases.

This commit is contained in:
David Wobrock 2020-09-27 15:47:27 +02:00 committed by Mariusz Felisiak
parent 01a7af09b9
commit 058747b77a
2 changed files with 20 additions and 2 deletions

View file

@ -1004,6 +1004,16 @@ class AggregateTestCase(TestCase):
self.assertEqual(author.sum_age, other_author.sum_age)
def test_aggregate_over_aggregate(self):
msg = "Cannot compute Avg('age'): 'age' is an aggregate"
with self.assertRaisesMessage(FieldError, msg):
Author.objects.annotate(
age_alias=F('age'),
).aggregate(
age=Sum(F('age')),
avg_age=Avg(F('age')),
)
def test_annotated_aggregate_over_annotated_aggregate(self):
with self.assertRaisesMessage(FieldError, "Cannot compute Sum('id__max'): 'id__max' is an aggregate"):
Book.objects.annotate(Max('id')).annotate(Sum('id__max'))