Skip to content

Commit c4f4bb7

Browse files
authored
django: fix iexact lookup with F expression crash
fixes #251
1 parent 8a9d0c4 commit c4f4bb7

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

‎packages/django-google-spanner/django_spanner/features.py‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
160160
# bitrightshift operator gives incorrect result:
161161
# https://github.com/orijtech/django-spanner/issues/335
162162
'expressions.tests.ExpressionOperatorTests.test_lefthand_bitwise_right_shift_operator',
163-
# using an F expression as the value of a REGEXP_CONTAINS lookup
164-
# crashes: https://github.com/orijtech/django-spanner/issues/251
165-
'expressions.tests.BasicExpressionsTests.test_ticket_11722_iexact_lookup',
166163
# integer division produces a float result, which can't be assigned to
167164
# an integer column:
168165
# https://github.com/orijtech/django-spanner/issues/331

‎packages/django-google-spanner/django_spanner/lookups.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ def iexact(self, compiler, connection):
3434
params.extend(rhs_params)
3535
rhs_sql = self.get_rhs_op(connection, rhs_sql)
3636
# Wrap the parameter in ^ and $ to restrict the regex to an exact match.
37-
params[0] = '^(?i)%s$' % params[0]
37+
if params:
38+
params[0] = '^(?i)%s$' % params[0]
39+
else:
40+
# If no params, then lhs_sql is the expression/column to use as the
41+
# regular expression. Use concat to make the value case-insensitive.
42+
lhs_sql = "CONCAT('^(?i)', " + lhs_sql + ", '$')"
43+
rhs_sql = rhs_sql.replace('%%s', '%s')
3844
# rhs_sql is REGEXP_CONTAINS(%s, %%s), and lhs_sql is the column name.
3945
return rhs_sql % lhs_sql, params
4046

0 commit comments

Comments
 (0)