From 61a62be313e395ce1265132bfc99f51476fb3c95 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Wed, 22 Apr 2026 11:46:49 -0400 Subject: Fixed #37057 -- Adjusted UniqueConstraint handling of UNKNOWN condition. When we adjusted UNKNOWN handling for CheckConstraint in refs #33996 we assumed that all usage of Q.check would benefit from this approach. However while CHECK constraints enforcement do ignore conditions involving NULL that resolve to UNKNOWN it's not the case for other type of constraints such as UNIQUE ones. Given how UNKNOWN should be treated depends on the callers context it appears that a better strategy for COALESCE wrapping is to force them to apply it if necessary. Thanks Drew Shapiro for the report. --- django/db/models/query_utils.py | 8 ++------ 1 file changed, 2 insertions(+), 6 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 c282c4f744..c37e6b7a49 100644 --- a/django/db/models/query_utils.py +++ b/django/db/models/query_utils.py @@ -165,8 +165,7 @@ class Q(tree.Node): matches against the expressions. """ # Avoid circular imports. - from django.db.models import BooleanField, Value - from django.db.models.functions import Coalesce + from django.db.models import Value from django.db.models.sql import Query from django.db.models.sql.constants import SINGLE @@ -178,10 +177,7 @@ class Q(tree.Node): query.add_annotation(Value(1), "_check") connection = connections[using] # This will raise a FieldError if a field is missing in "against". - if connection.features.supports_comparing_boolean_expr: - query.add_q(Q(Coalesce(self, True, output_field=BooleanField()))) - else: - query.add_q(self) + query.add_q(self) compiler = query.get_compiler(using=using) context_manager = ( transaction.atomic(using=using) -- cgit v1.3