Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.2.X] Fixed #13815 -- Ensure that reverse exclude lookups on nullab…

…le foreign keys exclude null values. Thanks to bpeschier for the report and patch.

Backport of r15458 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15460 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5e485a15ad07b492e648fe784ddd136d36ffcac4 1 parent bbfdb36
@freakboy3742 freakboy3742 authored
View
7 django/db/models/sql/query.py
@@ -1474,6 +1474,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)
View
12 tests/regressiontests/null_queries/tests.py
@@ -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>']
+ )
Please sign in to comment.
Something went wrong with that request. Please try again.