diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 43cb9d71b55..341765c6083 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -1475,6 +1475,13 @@ class Query(object): 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 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) diff --git a/tests/regressiontests/null_queries/tests.py b/tests/regressiontests/null_queries/tests.py index 72dcd5153cf..8392c588e4c 100644 --- a/tests/regressiontests/null_queries/tests.py +++ b/tests/regressiontests/null_queries/tests.py @@ -67,3 +67,15 @@ class NullQueriesTests(TestCase): [''] ) + # Ticket #13815: check if _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), + [''] + )