1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #23877 -- aggregation's subquery missed target col

Aggregation over subquery produced syntactically incorrect queries in
some cases as Django didn't ensure that source expressions of the
aggregation were present in the subquery.
This commit is contained in:
Anssi Kääriäinen
2014-11-20 14:30:25 +02:00
committed by Tim Graham
parent c7fd9b242d
commit bd337184f1
3 changed files with 81 additions and 7 deletions

View File

@@ -1168,3 +1168,30 @@ class JoinPromotionTests(TestCase):
def test_non_nullable_fk_not_promoted(self):
qs = Book.objects.annotate(Count('contact__name'))
self.assertIn(' INNER JOIN ', str(qs.query))
class AggregationOnRelationTest(TestCase):
def setUp(self):
self.a = Author.objects.create(name='Anssi', age=33)
self.p = Publisher.objects.create(name='Manning', num_awards=3)
Book.objects.create(isbn='asdf', name='Foo', pages=10, rating=0.1, price="0.0",
contact=self.a, publisher=self.p, pubdate=datetime.date.today())
def test_annotate_on_relation(self):
qs = Book.objects.annotate(avg_price=Avg('price'), publisher_name=F('publisher__name'))
self.assertEqual(qs[0].avg_price, 0.0)
self.assertEqual(qs[0].publisher_name, "Manning")
def test_aggregate_on_relation(self):
# A query with an existing annotation aggregation on a relation should
# succeed.
qs = Book.objects.annotate(avg_price=Avg('price')).aggregate(
publisher_awards=Sum('publisher__num_awards')
)
self.assertEqual(qs['publisher_awards'], 3)
Book.objects.create(isbn='asdf', name='Foo', pages=10, rating=0.1, price="0.0",
contact=self.a, publisher=self.p, pubdate=datetime.date.today())
qs = Book.objects.annotate(avg_price=Avg('price')).aggregate(
publisher_awards=Sum('publisher__num_awards')
)
self.assertEqual(qs['publisher_awards'], 6)