From 45bcc6feac68165eb3642d3c308c74b4828e679e Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Mon, 23 Dec 2019 12:17:56 +0100 Subject: [PATCH] Refs #31115 -- Added test for nested subquery that references related fields. Thanks Dmitriy Gunchenko for the report and Simon Charette for the analysis and tests. Regression in 5a4d7285bd10bd40d9f7e574a7c421eb21094858. Fixed in 5a4d7285bd10bd40d9f7e574a7c421eb21094858. --- tests/expressions/tests.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index ee8b2469667..1e6d4c967c4 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -538,6 +538,21 @@ class BasicExpressionsTests(TestCase): ) self.assertCountEqual(contrived.values_list(), outer.values_list()) + def test_nested_subquery_join_outer_ref(self): + inner = Employee.objects.filter(pk=OuterRef('ceo__pk')).values('pk') + qs = Employee.objects.annotate( + ceo_company=Subquery( + Company.objects.filter( + ceo__in=inner, + ceo__pk=OuterRef('pk'), + ).values('pk'), + ), + ) + self.assertSequenceEqual( + qs.values_list('ceo_company', flat=True), + [self.example_inc.pk, self.foobar_ltd.pk, self.gmbh.pk], + ) + def test_nested_subquery_outer_ref_2(self): first = Time.objects.create(time='09:00') second = Time.objects.create(time='17:00')