summaryrefslogtreecommitdiff
path: root/django/db/models/constraints.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/db/models/constraints.py')
-rw-r--r--django/db/models/constraints.py8
1 files changed, 7 insertions, 1 deletions
diff --git a/django/db/models/constraints.py b/django/db/models/constraints.py
index 7dfeb3b649..a4e8ab8ab1 100644
--- a/django/db/models/constraints.py
+++ b/django/db/models/constraints.py
@@ -6,6 +6,8 @@ from django.core.exceptions import FieldDoesNotExist, ValidationError
from django.db import connections
from django.db.models.constants import LOOKUP_SEP
from django.db.models.expressions import Exists, ExpressionList, F, RawSQL
+from django.db.models.fields import BooleanField
+from django.db.models.functions import Coalesce
from django.db.models.indexes import IndexExpression
from django.db.models.lookups import Exact, IsNull
from django.db.models.query_utils import Q
@@ -212,7 +214,11 @@ class CheckConstraint(BaseConstraint):
# Ignore constraints with excluded fields in condition.
if exclude and self._expression_refs_exclude(model, self.condition, exclude):
return
- if not Q(self.condition).check(against, using=using):
+ condition = self.condition
+ if connections[using].features.supports_comparing_boolean_expr:
+ # Checks constraints do not fail when they evaluate to UNKNOWN.
+ condition = Coalesce(condition, True, output_field=BooleanField())
+ if not Q(condition).check(against, using=using):
raise ValidationError(
self.get_violation_error_message(), code=self.violation_error_code
)