From e14d08cd894e9d91cb5d9f44ba7532c1a223f458 Mon Sep 17 00:00:00 2001 From: David Sanders Date: Fri, 9 Sep 2022 00:02:58 +1000 Subject: Fixed #33996 -- Fixed CheckConstraint validation on NULL values. Bug in 667105877e6723c6985399803a364848891513cc. Thanks James Beith for the report. --- django/db/models/query_utils.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'django/db/models/query_utils.py') diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py index 5562303e00..4a83fc380d 100644 --- a/django/db/models/query_utils.py +++ b/django/db/models/query_utils.py @@ -11,7 +11,7 @@ import logging from collections import namedtuple from django.core.exceptions import FieldError -from django.db import DEFAULT_DB_ALIAS, DatabaseError +from django.db import DEFAULT_DB_ALIAS, DatabaseError, connections from django.db.models.constants import LOOKUP_SEP from django.utils import tree @@ -115,7 +115,8 @@ class Q(tree.Node): matches against the expressions. """ # Avoid circular imports. - from django.db.models import Value + from django.db.models import BooleanField, Value + from django.db.models.functions import Coalesce from django.db.models.sql import Query from django.db.models.sql.constants import SINGLE @@ -126,7 +127,10 @@ class Q(tree.Node): query.add_annotation(value, name, select=False) query.add_annotation(Value(1), "_check") # This will raise a FieldError if a field is missing in "against". - query.add_q(self) + if connections[using].features.supports_comparing_boolean_expr: + query.add_q(Q(Coalesce(self, True, output_field=BooleanField()))) + else: + query.add_q(self) compiler = query.get_compiler(using=using) try: return compiler.execute_sql(SINGLE) is not None -- cgit v1.3