Skip to content

Commit

Permalink
Fixed #13815 -- Ensure that reverse exclude lookups on nullable forei…
Browse files Browse the repository at this point in the history
…gn keys exclude null values. Thanks to bpeschier for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15458 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed Feb 8, 2011
1 parent 340eade commit d3b38d5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions django/db/models/sql/query.py
Expand Up @@ -1475,6 +1475,13 @@ def split_exclude(self, filter_expr, prefix, can_reuse):
query.bump_prefix()
query.clear_ordering(True)
query.set_start(prefix)
# Adding extra check to make sure the selected field will not be null
# 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]
query.where.add((Constraint(alias, col, None), 'isnull', False), AND)

self.add_filter(('%s__in' % prefix, query), negate=True, trim=True,
can_reuse=can_reuse)

Expand Down
12 changes: 12 additions & 0 deletions tests/regressiontests/null_queries/tests.py
Expand Up @@ -67,3 +67,15 @@ def test_reverse_relations(self):
['<Inner: Inner object>']
)

# Ticket #13815: check if <reverse>_isnull=False does not produce
# faulty empty lists
objB = OuterB.objects.create(data="reverse")
self.assertQuerysetEqual(
OuterB.objects.filter(inner__isnull=False),
[]
)
Inner.objects.create(first=obj)
self.assertQuerysetEqual(
OuterB.objects.exclude(inner__isnull=False),
['<OuterB: OuterB object>']
)

0 comments on commit d3b38d5

Please sign in to comment.