1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #31415 -- Fixed crash when nested OuterRef is used with operators or in database functions.

This commit is contained in:
Hasan Ramezani 2020-04-03 11:05:10 +02:00 committed by Mariusz Felisiak
parent 89032876f4
commit 6fbce45b03
2 changed files with 20 additions and 1 deletions

View File

@ -585,6 +585,9 @@ class OuterRef(F):
return self.name
return ResolvedOuterRef(self.name)
def relabeled_clone(self, relabels):
return self
class Func(SQLiteNumericMixin, Expression):
"""An SQL function call."""

View File

@ -15,7 +15,7 @@ from django.db.models import (
)
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
@ -648,6 +648,22 @@ class BasicExpressionsTests(TestCase):
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(