diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index 2e831b21ac..a165923784 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -562,6 +562,9 @@ class ResolvedOuterRef(F): def relabeled_clone(self, relabels): return self + def get_group_by_cols(self, alias=None): + return [] + class OuterRef(F): def resolve_expression(self, *args, **kwargs): diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index 5e60c379a9..da6e04e8fd 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -687,6 +687,14 @@ class BasicExpressionsTests(TestCase): [self.foobar_ltd.ceo], ) + def test_subquery_group_by_outerref_in_filter(self): + inner = Company.objects.annotate( + employee=OuterRef('pk'), + ).values('employee').annotate( + min_num_chairs=Min('num_chairs'), + ).values('ceo') + self.assertIs(Employee.objects.filter(pk__in=Subquery(inner)).exists(), True) + def test_case_in_filter_if_boolean_output_field(self): is_ceo = Company.objects.filter(ceo=OuterRef('pk')) is_poc = Company.objects.filter(point_of_contact=OuterRef('pk'))