summaryrefslogtreecommitdiff
path: root/django/db/models/lookups.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2025-07-05 21:19:05 -0400
committerJacob Walls <jacobtylerwalls@gmail.com>2026-06-03 16:57:18 -0400
commit4bbc27c8686f10f9556cef02dbfa9f5157fbcf56 (patch)
tree61b13b5867e057caeb41102938a4d692874a7f87 /django/db/models/lookups.py
parentb5d9dbdf2bba8df4c85cd0db308b3a467d763d02 (diff)
Fixed #36492 -- Restored exact boolean lookup against literals on SQLite.
Performance regression in 37e6c5b on SQLite. Just like MySQL, and presumably Oracle, which don't have a native boolean type and incidently store booleans in integer columns, indices on such columns cannot be used when explicit boolean literal equalities are omitted. Adapt the logic introduced by refs #32691 for MySQL to be used for all backends that don't support native boolean fields instead of special casing MySQL, SQLite, and Oracle in their own special way. Note that review of this work surfaced that SQLite's query planner also cannot make use of indices when dealing with expressions of form WHERE NOT (indexed_bool_field = false) but that's a long standing problem unrelated to the restorative work performed in this patch. Thanks Klaas van Schelven for the report.
Diffstat (limited to 'django/db/models/lookups.py')
-rw-r--r--django/db/models/lookups.py15
1 files changed, 10 insertions, 5 deletions
diff --git a/django/db/models/lookups.py b/django/db/models/lookups.py
index 4c08999fb6..eef7bc93a5 100644
--- a/django/db/models/lookups.py
+++ b/django/db/models/lookups.py
@@ -151,11 +151,16 @@ class Lookup(Expression):
# expression unless they're wrapped in a CASE WHEN.
wrapped = False
exprs = []
- for expr in (self.lhs, self.rhs):
- if connection.ops.conditional_expression_supported_in_where_clause(expr):
- expr = Case(When(expr, then=True), default=False)
- wrapped = True
- exprs.append(expr)
+ if getattr(self.lhs, "conditional", False) and getattr(
+ self.rhs, "conditional", False
+ ):
+ for expr in (self.lhs, self.rhs):
+ if connection.ops.conditional_expression_supported_in_where_clause(
+ expr
+ ):
+ expr = Case(When(expr, then=True), default=False)
+ wrapped = True
+ exprs.append(expr)
lookup = type(self)(*exprs) if wrapped else self
return lookup.as_sql(compiler, connection)