Skip to content

delete(part_integrity="enforce") commits a delete that removes some of a master's parts when the cascade also removes other rows of the master table #1760

Backlog

Description

@dimitri-yatsenko

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

Activity

  1. added this to the v2.4 milestone on Nov 25, 2026
  2. added
    bug
    on Nov 25, 2026
  3. modified the milestones: v2.4, v2.3.7 on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions