From 2a431db0f5e91110b4fda05949de1f158a20ec5b Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Wed, 27 Mar 2019 08:24:05 +0100 Subject: [PATCH] Fixed #28621 -- Fixed crash of annotations with OuterRef. --- django/db/models/expressions.py | 2 ++ tests/expressions/tests.py | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index 37818850be..16df317631 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -527,6 +527,8 @@ class ResolvedOuterRef(F): In this case, the reference to the outer query has been resolved because the inner query has been used as a subquery. """ + contains_aggregate = False + def as_sql(self, *args, **kwargs): raise ValueError( 'This queryset contains a reference to an outer query and may ' diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index c19178b3ca..c20b88ff83 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -591,6 +591,17 @@ class BasicExpressionsTests(TestCase): outer = Company.objects.filter(pk__in=Subquery(inner.values('pk'))) self.assertEqual(outer.get().name, 'Test GmbH') + def test_annotation_with_outerref(self): + gmbh_salary = Company.objects.annotate( + max_ceo_salary_raise=Subquery( + Company.objects.annotate( + salary_raise=OuterRef('num_employees') + F('num_employees'), + ).order_by('-salary_raise').values('salary_raise')[:1], + output_field=models.IntegerField(), + ), + ).get(pk=self.gmbh.pk) + self.assertEqual(gmbh_salary.max_ceo_salary_raise, 2332) + def test_pickle_expression(self): expr = Value(1, output_field=models.IntegerField()) expr.convert_value # populate cached property