Skip to content

Commit 05335d1

Browse files
fix(cascade): keep a % in a delete's restriction
A delete restricted by a condition containing %, such as `(Label & "name = '50%'").delete()` or a dict `{"name": "50%"}`, deleted nothing and returned 0. The seed's conditions are compiled SQL, in which each % is already escaped as %% for the database driver. The cascade applies them again through `restrict()`, which escaped every % a second time, so the condition looked for the literal text `50%%` and matched no row. A LIKE pattern kept working only because `%%` and `%` match the same strings there. The seed's conditions now have that first escape undone before the cascade applies them again, so they select the same rows as the restriction on its own. The new test checks the preview counts for a string condition, a dict and a condition holding `%%`, and deletes with a LIKE pattern, on both backends. It fails before this change.
1 parent 89afc8a commit 05335d1

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎src/datajoint/diagram.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,11 @@ def _seed_restriction(table_expr):
181181
"""
182182
if table_expr._top is not None or any(_has_subquery(c) for c in table_expr.restriction):
183183
return AndList([table_expr.keys()]), set(table_expr.primary_key)
184-
return AndList(table_expr.restriction), set(table_expr.restriction_attributes)
184+
# The seed's conditions are compiled SQL, with each % already escaped as %%
185+
# for the driver. The traversal applies them again through restrict(), which
186+
# escapes again, so the escape is undone here.
187+
restriction = AndList(c.replace("%%", "%") if isinstance(c, str) else c for c in table_expr.restriction)
188+
return restriction, set(table_expr.restriction_attributes)
185189

186190

187191
class Diagram(nx.MultiDiGraph): # noqa: C901

‎tests/integration/test_cascade_overdelete.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,38 @@ class Session(dj.Manual):
290290
seed.delete(prompt=False)
291291
assert set(Subject.to_arrays("subject_id").tolist()) == {1, 2, 3, 4} - subjects
292292
assert set(zip(*Session.to_arrays("subject_id", "session_id"))) == all_sessions - sessions
293+
294+
295+
def test_seed_condition_with_a_percent_sign(schema_by_backend):
296+
"""A seed condition containing % selects the same rows in the cascade as
297+
on its own."""
298+
299+
@schema_by_backend
300+
class Label(dj.Manual):
301+
definition = """
302+
label_id : int32
303+
---
304+
name : varchar(16)
305+
"""
306+
307+
@schema_by_backend
308+
class Use(dj.Manual):
309+
definition = """
310+
-> Label
311+
use_id : int32
312+
"""
313+
314+
Label.insert([(1, "50%"), (2, "other"), (3, "5%%")])
315+
Use.insert([(1, 1), (2, 1), (3, 1)])
316+
317+
for seed, label in [
318+
(Label & "name = '50%'", 1),
319+
(Label & {"name": "50%"}, 1),
320+
(Label & "name = '5%%'", 3),
321+
]:
322+
assert len(seed) == 1
323+
assert dj.Diagram.cascade(seed).counts()[Use.full_table_name] == 1
324+
325+
(Label & "name LIKE '50%'").delete(prompt=False)
326+
assert sorted(Label.to_arrays("label_id").tolist()) == [2, 3]
327+
assert sorted(Use.to_arrays("label_id").tolist()) == [2, 3]

0 commit comments

Comments
 (0)