Skip to content

Commit

Permalink
Fixed #31415 -- Fixed crash when nested OuterRef is used with operato…
Browse files Browse the repository at this point in the history
…rs or in database functions.
  • Loading branch information
hramezani authored and felixxm committed Apr 4, 2020
1 parent 8903287 commit 6fbce45
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions django/db/models/expressions.py
Expand Up @@ -585,6 +585,9 @@ def resolve_expression(self, *args, **kwargs):
return self.name
return ResolvedOuterRef(self.name)

def relabeled_clone(self, relabels):
return self


class Func(SQLiteNumericMixin, Expression):
"""An SQL function call."""
Expand Down
18 changes: 17 additions & 1 deletion tests/expressions/tests.py
Expand Up @@ -15,7 +15,7 @@
)
from django.db.models.expressions import Col, Combinable, Random, RawSQL, Ref
from django.db.models.functions import (
Coalesce, Concat, Length, Lower, Substr, Upper,
Coalesce, Concat, Left, Length, Lower, Substr, Upper,
)
from django.db.models.sql import constants
from django.db.models.sql.datastructures import Join
Expand Down Expand Up @@ -648,6 +648,22 @@ def test_outerref_with_operator(self):
outer = Company.objects.filter(pk__in=Subquery(inner.values('pk')))
self.assertEqual(outer.get().name, 'Test GmbH')

def test_nested_outerref_with_function(self):
self.gmbh.point_of_contact = Employee.objects.get(lastname='Meyer')
self.gmbh.save()
inner = Employee.objects.filter(
lastname__startswith=Left(OuterRef(OuterRef('lastname')), 1),
)
qs = Employee.objects.annotate(
ceo_company=Subquery(
Company.objects.filter(
point_of_contact__in=inner,
ceo__pk=OuterRef('pk'),
).values('name'),
),
).filter(ceo_company__isnull=False)
self.assertEqual(qs.get().ceo_company, 'Test GmbH')

def test_annotation_with_outerref(self):
gmbh_salary = Company.objects.annotate(
max_ceo_salary_raise=Subquery(
Expand Down

0 comments on commit 6fbce45

Please sign in to comment.