Skip to content

Commit 8a8423b

Browse files
fix: Escape % in LIKE patterns for MySQL
PyMySQL uses % for parameter placeholders, so the wildcard % in LIKE patterns needs to be doubled (%%) for MySQL. PostgreSQL doesn't need this escaping. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent be7d079 commit 8a8423b

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/datajoint/dependencies.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,24 @@ def load(self, force: bool = True) -> None:
160160
# Backend-specific table name concatenation
161161
# MySQL: concat('`', table_schema, '`.`', table_name, '`')
162162
# PostgreSQL: '"' || table_schema || '"."' || table_name || '"'
163+
# Note: MySQL uses %% to escape % in LIKE patterns (PyMySQL format strings)
163164
if adapter.backend == "mysql":
164165
tab_expr = "concat('`', table_schema, '`.`', table_name, '`')"
165166
ref_tab_expr = "concat('`', referenced_table_schema, '`.`', referenced_table_name, '`')"
167+
like_pattern = "'~%%'" # Double %% for PyMySQL escaping
166168
else:
167169
# PostgreSQL
168170
tab_expr = "'\"' || table_schema || '\".\"' || table_name || '\"'"
169171
ref_tab_expr = "'\"' || referenced_table_schema || '\".\"' || referenced_table_name || '\"'"
172+
like_pattern = "'~%'" # PostgreSQL doesn't need escaping
170173

171174
# load primary key info
172175
keys = self._conn.query(
173176
f"""
174177
SELECT
175178
{tab_expr} as tab, column_name
176179
FROM information_schema.key_column_usage
177-
WHERE table_name NOT LIKE '~%' AND table_schema in ({schemas_list}) AND constraint_name='PRIMARY'
180+
WHERE table_name NOT LIKE {like_pattern} AND table_schema in ({schemas_list}) AND constraint_name='PRIMARY'
178181
"""
179182
)
180183
pks = defaultdict(set)
@@ -195,7 +198,7 @@ def load(self, force: bool = True) -> None:
195198
{ref_tab_expr} as referenced_table,
196199
column_name, referenced_column_name
197200
FROM information_schema.key_column_usage
198-
WHERE referenced_table_name NOT LIKE '~%' AND (referenced_table_schema in ({schemas_list}) OR
201+
WHERE referenced_table_name NOT LIKE {like_pattern} AND (referenced_table_schema in ({schemas_list}) OR
199202
referenced_table_schema is not NULL AND table_schema in ({schemas_list}))
200203
""",
201204
as_dict=True,

0 commit comments

Comments
 (0)