summaryrefslogtreecommitdiff
path: root/django/db
diff options
context:
space:
mode:
authorDavid Sanders <shang.xiao.sanders@gmail.com>2022-09-09 00:02:58 +1000
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-09-13 12:48:31 +0200
commite14d08cd894e9d91cb5d9f44ba7532c1a223f458 (patch)
treea524445fadbda60124a63468c643f31639d794fc /django/db
parentb731e8841558ee4caaba766c83f34ea9c7004f8b (diff)
Fixed #33996 -- Fixed CheckConstraint validation on NULL values.
Bug in 667105877e6723c6985399803a364848891513cc. Thanks James Beith for the report.
Diffstat (limited to 'django/db')
-rw-r--r--django/db/backends/base/features.py3
-rw-r--r--django/db/backends/oracle/features.py1
-rw-r--r--django/db/models/query_utils.py10
3 files changed, 11 insertions, 3 deletions
diff --git a/django/db/backends/base/features.py b/django/db/backends/base/features.py
index c54d30cf73..4fd21beee3 100644
--- a/django/db/backends/base/features.py
+++ b/django/db/backends/base/features.py
@@ -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
diff --git a/django/db/backends/oracle/features.py b/django/db/backends/oracle/features.py
index 289f786f5e..49e58ff59d 100644
--- a/django/db/backends/oracle/features.py
+++ b/django/db/backends/oracle/features.py
@@ -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
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