Refs #32673, Refs #35295 -- Avoided wrapping rhs direct values in lookups.

This commit is contained in:
Mariusz Felisiak 2024-03-13 17:46:37 +01:00 committed by GitHub
parent 80fe2f4391
commit 33c06ca0da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 1 deletions

View File

@ -122,7 +122,7 @@ class Lookup(Expression):
# Ensure expression is wrapped in parentheses to respect operator
# precedence but avoid double wrapping as it can be misinterpreted
# on some backends (e.g. subqueries on SQLite).
if sql and sql[0] != "(":
if not isinstance(value, Value) and sql and sql[0] != "(":
sql = "(%s)" % sql
return sql, params
else:

View File

@ -1366,6 +1366,12 @@ class LookupTests(TestCase):
[stock_1, stock_2],
)
def test_lookup_direct_value_rhs_unwrapped(self):
with self.assertNumQueries(1) as ctx:
self.assertIs(Author.objects.filter(GreaterThan(2, 1)).exists(), True)
# Direct values on RHS are not wrapped.
self.assertIn("2 > 1", ctx.captured_queries[0]["sql"])
class LookupQueryingTests(TestCase):
@classmethod