Skip to content

Commit

Permalink
Refs #32673, Refs #35295 -- Avoided wrapping rhs direct values in loo…
Browse files Browse the repository at this point in the history
…kups.
  • Loading branch information
felixxm authored Mar 13, 2024
1 parent 80fe2f4 commit 33c06ca
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django/db/models/lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def process_rhs(self, compiler, connection):
# 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:
Expand Down
6 changes: 6 additions & 0 deletions tests/lookup/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,12 @@ def test_lookup_rhs(self):
[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
Expand Down

0 comments on commit 33c06ca

Please sign in to comment.