Repository navigation
Description
Problem
With part_integrity="enforce", the default, delete() can commit a delete that removes some of a master's parts and keeps the master. The check runs after the deletes and compares tables, not rows (table.py, the post-check after the delete loop). It refuses only when a part table lost rows and its master table lost none. When the cascade also removes other rows of the master table, the check passes, and a master row that keeps its key loses some of its parts.
Reproduction (master, MySQL)
@schema
class Probe(dj.Manual):
definition = """
probe : int32
"""
@schema
class Group(dj.Manual):
definition = """
group_id : int32
---
-> Probe.proj(lead="probe")
"""
class Member(dj.Part):
definition = """
-> master
-> Probe
"""
Probe.insert([{"probe": 1}, {"probe": 2}])
Group.insert([{"group_id": 7, "lead": 2}, {"group_id": 8, "lead": 1}])
Group.Member.insert([
{"group_id": 7, "probe": 1},
{"group_id": 7, "probe": 2},
{"group_id": 8, "probe": 2},
])
(Probe & {"probe": 1}).delete(prompt=False) # part_integrity="enforce"The delete commits. Group 8 is removed with its parts, because its lead is probe 1. Group 7 stays, but it has lost its member (7, 1), which referenced probe 1:
Group: [{'group_id': 7, 'lead': 2}]
Group.Member: [{'group_id': 7, 'probe': 2}]
The same delete without the lead reference, so that no group is removed, is refused as expected ("Attempt to delete part table ... before its master").
Expected
enforce refuses, before anything is deleted, a delete in which any reached part row's master row is not also reached. The check is per row: for each part table P in the cascade with master M, the part rows that would lose their master are d[P].proj() - d[M].proj(), or all of d[P] when M isn't in the cascade. d[T] is the cascade's restricted table for T. The delete is refused when any such set is non-empty, and the refusal names the master rows (M.proj() & orphans). part_integrity="cascade" is the alternative: it pulls those masters, and their other parts, into the delete.
The check belongs where the cascade is built, so that a preview (dj.Diagram.cascade(..., part_integrity="enforce")) and the delete give the same answer before any row is touched, without a rollback.
Notes
- A part's master is matched on the master's primary key. That holds when the part references its master directly; a renamed reference, or a part of a part, depends on how parts are recognized (fix(declare): refuse a part reference that closes a master-part group cycle #1620 defines owned parts by a rule).
- In schemas whose names contain
-, no part is recognized today, so neither the current check norcascade's lift runs (Part tables are not recognized in schemas whose names contain non-word characters (e.g. hyphens), so part_integrity checks are skipped #1719, fixed by Recognize part tables in schemas whose names contain hyphens or other non-word characters #1728). - 2.4's traversal planner (
Diagram.expand, feat(diagram): Diagram.expand on a traversal plan #1637) computes the closure with the lift. With the planner,enforcecan be stated as: refuse when the closure without the lift differs from the closure with it.
Activity