1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Refs #33482 -- Fixed QuerySet selecting and filtering againts Exists() with empty queryset.

Thanks Tobias Bengfort for the report.
This commit is contained in:
Simon Charette 2023-10-04 15:30:50 -04:00 committed by GitHub
parent 0989cf13e7
commit ea596a52d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -1624,6 +1624,15 @@ class Exists(Subquery):
sql = "CASE WHEN {} THEN 1 ELSE 0 END".format(sql)
return sql, params
def as_sql(self, compiler, *args, **kwargs):
try:
return super().as_sql(compiler, *args, **kwargs)
except EmptyResultSet:
features = compiler.connection.features
if not features.supports_boolean_expr_in_select_clause:
return "1=0", ()
return compiler.compile(Value(False))
@deconstructible(path="django.db.models.OrderBy")
class OrderBy(Expression):

View File

@ -2294,6 +2294,14 @@ class ExistsTests(TestCase):
self.assertSequenceEqual(qs, [manager])
self.assertIs(qs.get().not_exists, True)
def test_filter_by_empty_exists(self):
manager = Manager.objects.create()
qs = Manager.objects.annotate(exists=Exists(Manager.objects.none())).filter(
pk=manager.pk, exists=False
)
self.assertSequenceEqual(qs, [manager])
self.assertIs(qs.get().exists, False)
class FieldTransformTests(TestCase):
@classmethod