[2.0.x] Fixed #28689 -- Fixed unquoted table names in Subquery SQL when using OuterRef.

Regression in f48bc7c3db.

Backport of 81e357a7e1 from master
This commit is contained in:
Mariusz Felisiak 2017-10-25 21:52:38 +02:00 committed by Tim Graham
parent 01987bc0b4
commit f5184ae5dc
4 changed files with 10 additions and 1 deletions

View File

@ -997,7 +997,7 @@ class Subquery(Expression):
)
# Add table alias to the parent query's aliases to prevent
# quoting.
if hasattr(resolved, 'alias'):
if hasattr(resolved, 'alias') and resolved.alias != resolved.target.model._meta.db_table:
clone.queryset.query.external_aliases.add(resolved.alias)
return resolved
return child

View File

@ -16,3 +16,6 @@ Bugfixes
* Made ``QuerySet.reverse()`` affect ``nulls_first`` and ``nulls_last``
(:ticket:`28722`).
* Fixed unquoted table names in ``Subquery`` SQL when using ``OuterRef``
(:ticket:`28689`).

View File

@ -50,6 +50,7 @@ class Experiment(models.Model):
end = models.DateTimeField()
class Meta:
db_table = 'expressions_ExPeRiMeNt'
ordering = ('name',)
def duration(self):

View File

@ -545,6 +545,11 @@ class BasicExpressionsTests(TestCase):
expr = FuncB(FuncA())
self.assertEqual(expr.output_field, FuncA.output_field)
def test_outerref_mixed_case_table_name(self):
inner = Result.objects.filter(result_time__gte=OuterRef('experiment__assigned'))
outer = Result.objects.filter(pk__in=Subquery(inner.values('pk')))
self.assertFalse(outer.exists())
class IterableLookupInnerExpressionsTests(TestCase):
@classmethod