summaryrefslogtreecommitdiff
path: root/django/db/models/sql/query.py
diff options
context:
space:
mode:
authorAnssi Kääriäinen <akaariai@gmail.com>2013-11-02 22:35:45 +0200
committerAnssi Kääriäinen <akaariai@gmail.com>2013-11-02 22:35:45 +0200
commitbec0b2a8c637ba2dc3745ad0c1e68ccc6c2f78bd (patch)
tree6798d30312529877b04af916154322c1ac26b0fc /django/db/models/sql/query.py
parent7548aa8ffd46eb6e0f73730d1b2eb515ba581f95 (diff)
Fixed #14511 -- bug in .exclude() query
Diffstat (limited to 'django/db/models/sql/query.py')
-rw-r--r--django/db/models/sql/query.py17
1 files changed, 15 insertions, 2 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 493856fb41..46a308f5ce 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -22,7 +22,7 @@ from django.db.models.related import PathInfo
from django.db.models.sql import aggregates as base_aggregates_module
from django.db.models.sql.constants import (QUERY_TERMS, ORDER_DIR, SINGLE,
ORDER_PATTERN, JoinInfo, SelectInfo)
-from django.db.models.sql.datastructures import EmptyResultSet, Empty, MultiJoin
+from django.db.models.sql.datastructures import EmptyResultSet, Empty, MultiJoin, Col
from django.db.models.sql.expressions import SQLEvaluator
from django.db.models.sql.where import (WhereNode, Constraint, EverythingNode,
ExtraWhere, AND, OR, EmptyWhere)
@@ -820,6 +820,9 @@ class Query(object):
conflict. Even tables that previously had no alias will get an alias
after this call.
"""
+ if self.alias_prefix != outer_query.alias_prefix:
+ # No clashes between self and outer query should be possible.
+ return
self.alias_prefix = chr(ord(self.alias_prefix) + 1)
while self.alias_prefix in self.subq_aliases:
self.alias_prefix = chr(ord(self.alias_prefix) + 1)
@@ -1514,9 +1517,19 @@ class Query(object):
# since we are adding a IN <subquery> clause. This prevents the
# database from tripping over IN (...,NULL,...) selects and returning
# nothing
+ alias, col = query.select[0].col
if self.is_nullable(query.select[0].field):
- alias, col = query.select[0].col
query.where.add((Constraint(alias, col, query.select[0].field), 'isnull', False), AND)
+ if alias in can_reuse:
+ pk = query.select[0].field.model._meta.pk
+ # Need to add a restriction so that outer query's filters are in effect for
+ # the subquery, too.
+ query.bump_prefix(self)
+ query.where.add(
+ (Constraint(query.select[0].col[0], pk.column, pk),
+ 'exact', Col(alias, pk.column)),
+ AND
+ )
condition = self.build_filter(
('%s__in' % trimmed_prefix, query),