diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index bbe310c8c3..0180f6b6cc 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -786,8 +786,7 @@ class SQLCompiler(object): return list(result) return result - def as_subquery_condition(self, alias, columns): - qn = self.quote_name_unless_alias + def as_subquery_condition(self, alias, columns, qn): qn2 = self.connection.ops.quote_name if len(columns) == 1: sql, params = self.as_sql() diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py index 08f9d6c837..2a342d417a 100644 --- a/django/db/models/sql/where.py +++ b/django/db/models/sql/where.py @@ -408,9 +408,12 @@ class SubqueryConstraint(object): query.clear_ordering(True) query_compiler = query.get_compiler(connection=connection) - return query_compiler.as_subquery_condition(self.alias, self.columns) + return query_compiler.as_subquery_condition(self.alias, self.columns, qn) - def relabeled_clone(self, relabels): + def relabel_aliases(self, change_map): + self.alias = change_map.get(self.alias, self.alias) + + def clone(self): return self.__class__( - relabels.get(self.alias, self.alias), - self.columns, self.query_object) + self.alias, self.columns, self.targets, + self.query_object) diff --git a/tests/queries/tests.py b/tests/queries/tests.py index 68a06abb49..2d6fc0b008 100644 --- a/tests/queries/tests.py +++ b/tests/queries/tests.py @@ -17,7 +17,7 @@ from django.utils import unittest from django.utils.datastructures import SortedDict from .models import (Annotation, Article, Author, Celebrity, Child, Cover, - Detail, DumbCategory, ExtraInfo, Fan, Item, LeafA, LoopX, LoopZ, + Detail, DumbCategory, ExtraInfo, Fan, Item, LeafA, Join, LeafB, LoopX, LoopZ, ManagedModel, Member, NamedCategory, Note, Number, Plaything, PointerA, Ranking, Related, Report, ReservedName, Tag, TvChef, Valid, X, Food, Eaten, Node, ObjectA, ObjectB, ObjectC, CategoryItem, SimpleCategory, @@ -2827,3 +2827,17 @@ class ValuesSubqueryTests(TestCase): self.assertQuerysetEqual( Order.objects.filter(items__in=OrderItem.objects.values_list('status')), [o1.pk], lambda x: x.pk) + +class DoubleInSubqueryTests(TestCase): + def test_double_subquery_in(self): + lfa1 = LeafA.objects.create(data='foo') + lfa2 = LeafA.objects.create(data='bar') + lfb1 = LeafB.objects.create(data='lfb1') + lfb2 = LeafB.objects.create(data='lfb2') + Join.objects.create(a=lfa1, b=lfb1) + Join.objects.create(a=lfa2, b=lfb2) + leaf_as = LeafA.objects.filter(data='foo').values_list('pk', flat=True) + joins = Join.objects.filter(a__in=leaf_as).values_list('b__id', flat=True) + qs = LeafB.objects.filter(pk__in=joins) + self.assertQuerysetEqual( + qs, [lfb1], lambda x: x)