Fixed #33482 -- Fixed QuerySet filtering againts negated Exists() with empty queryset.
Thanks Tobias Bengfort for the report.
This commit is contained in:
parent
770d3e6a4c
commit
b7d1da5a62
|
@ -1211,13 +1211,18 @@ class Exists(Subquery):
|
||||||
|
|
||||||
def as_sql(self, compiler, connection, template=None, **extra_context):
|
def as_sql(self, compiler, connection, template=None, **extra_context):
|
||||||
query = self.query.exists(using=connection.alias)
|
query = self.query.exists(using=connection.alias)
|
||||||
sql, params = super().as_sql(
|
try:
|
||||||
compiler,
|
sql, params = super().as_sql(
|
||||||
connection,
|
compiler,
|
||||||
template=template,
|
connection,
|
||||||
query=query,
|
template=template,
|
||||||
**extra_context,
|
query=query,
|
||||||
)
|
**extra_context,
|
||||||
|
)
|
||||||
|
except EmptyResultSet:
|
||||||
|
if self.negated:
|
||||||
|
return '', ()
|
||||||
|
raise
|
||||||
if self.negated:
|
if self.negated:
|
||||||
sql = 'NOT {}'.format(sql)
|
sql = 'NOT {}'.format(sql)
|
||||||
return sql, params
|
return sql, params
|
||||||
|
|
|
@ -1905,6 +1905,13 @@ class ExistsTests(TestCase):
|
||||||
)
|
)
|
||||||
self.assertNotIn('ORDER BY', captured_sql)
|
self.assertNotIn('ORDER BY', captured_sql)
|
||||||
|
|
||||||
|
def test_negated_empty_exists(self):
|
||||||
|
manager = Manager.objects.create()
|
||||||
|
qs = Manager.objects.filter(
|
||||||
|
~Exists(Manager.objects.none()) & Q(pk=manager.pk)
|
||||||
|
)
|
||||||
|
self.assertSequenceEqual(qs, [manager])
|
||||||
|
|
||||||
|
|
||||||
class FieldTransformTests(TestCase):
|
class FieldTransformTests(TestCase):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue