mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Fixed #33996 -- Fixed CheckConstraint validation on NULL values.
Bug in 667105877e.
Thanks James Beith for the report.
This commit is contained in:
committed by
Mariusz Felisiak
parent
b731e88415
commit
e14d08cd89
@@ -302,6 +302,9 @@ class BaseDatabaseFeatures:
|
||||
# Does the backend support boolean expressions in SELECT and GROUP BY
|
||||
# clauses?
|
||||
supports_boolean_expr_in_select_clause = True
|
||||
# Does the backend support comparing boolean expressions in WHERE clauses?
|
||||
# Eg: WHERE (price > 0) IS NOT NULL
|
||||
supports_comparing_boolean_expr = True
|
||||
|
||||
# Does the backend support JSONField?
|
||||
supports_json_field = True
|
||||
|
||||
@@ -71,6 +71,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
|
||||
supports_slicing_ordering_in_compound = True
|
||||
allows_multiple_constraints_on_same_fields = False
|
||||
supports_boolean_expr_in_select_clause = False
|
||||
supports_comparing_boolean_expr = False
|
||||
supports_primitives_in_json_field = False
|
||||
supports_json_field_contains = False
|
||||
supports_collation_on_textfield = False
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user