summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Schinckel <matt@schinckel.net>2017-02-25 21:11:56 +1030
committerTim Graham <timograham@gmail.com>2017-03-01 07:56:37 -0500
commitf48bc7c3dbd204eefb3c19016b1e4906ac26bee3 (patch)
treea2bd9e6a5829cdd515049d88430d8627c54c4c21
parent9f21e35100b022b31fe723323db85c2cb28b16b8 (diff)
Fixed #27862 -- Fixed incorrectly quoted table aliases in Subquery SQL.
Add aliases from resolved querysets to the parent query's external aliases to prevent those aliases from being quoted. Thanks to Vasily Stepanov for the report and Tim Graham for the review.
-rw-r--r--django/db/models/expressions.py7
-rw-r--r--tests/expressions/tests.py9
2 files changed, 15 insertions, 1 deletions
diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py
index 8361e42d20..ed57599a15 100644
--- a/django/db/models/expressions.py
+++ b/django/db/models/expressions.py
@@ -940,10 +940,15 @@ class Subquery(Expression):
def resolve(child):
if hasattr(child, 'resolve_expression'):
- return child.resolve_expression(
+ resolved = child.resolve_expression(
query=query, allow_joins=allow_joins, reuse=reuse,
summarize=summarize, for_save=for_save,
)
+ # Add table alias to the parent query's aliases to prevent
+ # quoting.
+ if hasattr(resolved, 'alias'):
+ clone.queryset.query.external_aliases.add(resolved.alias)
+ return resolved
return child
resolve_all(clone.queryset.query.where)
diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py
index 38258e0dd0..5483a17fd1 100644
--- a/tests/expressions/tests.py
+++ b/tests/expressions/tests.py
@@ -516,6 +516,15 @@ class BasicExpressionsTests(TestCase):
[{'salary': 10, 'total_employees': 2300}, {'salary': 20, 'total_employees': 35}],
)
+ def test_subquery_references_joined_table_twice(self):
+ inner = Company.objects.filter(
+ num_chairs__gte=OuterRef('ceo__salary'),
+ num_employees__gte=OuterRef('point_of_contact__salary'),
+ )
+ # Another contrived example (there is no need to have a subquery here)
+ outer = Company.objects.filter(pk__in=Subquery(inner.values('pk')))
+ self.assertFalse(outer.exists())
+
class IterableLookupInnerExpressionsTests(TestCase):
@classmethod