Master–part: reject parts the master does not own, and insert part rows only with their master

未關閉
#1,768 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 5 天內回覆

還沒有人認領這個 Issue。

評估

難度
5/5
預估耗時
一週以上
新手友好度
28/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
python

研究方向

Start with item 1: read the insert guard in src/datajoint/table.py and the Part implementation in src/datajoint/user_tables.py, then examine tests/integration/test_autopopulate.py. The first slice is done when computed-part inserts inherit the master’s _allow_insert behavior and a part-specific integration test passes. The remaining items depend on other issues and span transactions, ownership checks, cascades, and specification updates.

由索引模型根據 Issue 內容生成。

描述

A master row and its part rows are one entity. A part belongs to its master through a dependency in the part's primary key, has its master's tier, and is written in the transaction that writes its master row. A table that depends on the master can then read the whole set of parts and know it is complete and final. The docs state this rule (Relational Workflow Model, "Workflow steps and table tiers": a part "inherits its master's tier and is written in the same transaction"; AutoPopulate spec §4.3, rule 1: "Direct inserts into an auto-populated table are rejected at runtime"). The library holds it only for the rows populate() writes inside make(). On every other path a part row can be added to an existing master at any time, so a result computed from "all parts of this master" can be computed from a partial set, or stay in place after its inputs changed, with nothing recording either.

Items 1 to 4 were read, not run: each comes from reading master at b2994e5e and its tests, and each sketch is the failing test to write first. The schema-name case in item 4 has an executed reproduction in #1719. Entry, Ingest, and Compute are the tier names. In current code the classes are dj.Manual, dj.Imported, and dj.Computed, and dj.Entry, dj.Ingest, and dj.Compute name the same classes (#1546); the sketches use the new names.

Inserts, declarations, and spec text that separate a part from its master

The sketches for items 1 and 2 share these tables:

@schema
class Session(dj.Entry):
    definition = """
    session_id : int32
    """

    class Trial(dj.Part):
        definition = """
        -> master
        trial_id : int32
        """


@schema
class Segmentation(dj.Compute):
    definition = """
    -> Session
    """

    class Roi(dj.Part):
        definition = """
        -> master
        roi_id : int32
        """

    def make(self, key):
        self.insert1(key)
        self.Roi.insert1({**key, "roi_id": 1})
  • 1. Parts of Compute and Ingest masters accept direct inserts outside make(). Segmentation.Roi.insert(rows) succeeds for any existing master key without allow_direct_insert=True, so a computed result gains rows that no make() produced.

    • Where: the guard at src/datajoint/table.py:874 reads getattr(self, "_allow_insert", True). Only AutoPopulate defines the flag (autopopulate.py:105, set on the master class at :682 and :753); Part (user_tables.py:196-262) defines none. test_allow_insert (tests/integration/test_autopopulate.py:106) pins the guard for masters, and no test covers a part.
    • Change: give Part an _allow_insert property that returns getattr(self._master, "_allow_insert", True), and add a part case beside test_allow_insert. The flag is set per class, so inside make() a part row for another master key still passes.
    • Compatibility: breaking for code that back-fills computed parts directly; allow_direct_insert=True restores it. Warn for one minor release first. A dj.FreeTable opened on an auto-populated table, master or part, also passes the guard because FreeTable defines no flag; that bypass is tracked separately.
    Session.insert1({"session_id": 1})
    Segmentation.populate()
    Segmentation.Roi.insert1({"session_id": 1, "roi_id": 2})
    # accepted today; expected: DataJointError, as Segmentation.insert1 raises
    
  • 2. Parts of an Entry master can be inserted in any later transaction. A populate() that runs between the master's commit and the parts' commit computes from a partial set, and a part row added afterwards changes the inputs of results that stay in place.

    • Where: Part has no insert override (user_tables.py:196-262), and table.py:874 is the only insert guard. The master-part spec §2.1 calls atomicity for these masters "a convention rather than an enforced guarantee". tests/integration/test_cascade_delete.py:330-332 inserts a master, then its parts, as separate statements.
    • Change: first step, Part.insert on a part of an Entry or Lookup master raises unless self.connection.in_transaction, with an explicit override for repairs. That checks only that a transaction is open. The full rule also requires that the open transaction inserted the referenced master rows, which needs the inserted master keys recorded per transaction on the connection. #711 (closed for inactivity) proposed one call that inserts a master with its parts.
    • Compatibility: breaking for every script that inserts an Entry master and its parts as separate statements. Warn for one minor release, then raise.
    Session.insert1({"session_id": 2})
    Session.Trial.insert1({"session_id": 2, "trial_id": 1})
    # accepted today; expected: DataJointError
    
    with schema.connection.transaction:      # passes before and after the change
        Session.insert1({"session_id": 3})
        Session.Trial.insert1({"session_id": 3, "trial_id": 1})
    
  • 3. A part that its master does not own is declared without error. A dj.Part whose primary key carries no dependency on its master or on a sibling part (the reference sits below ---, or is absent) has a key that identifies no master row, yet delete, drop, and the cascade treat it as a part.

    • Where: Part overrides no _declare_check, and the base hook is empty (table.py:203-217); schemas.py:264 binds the master by class nesting alone. test_upward_u3_nonprimary_master_fk (tests/integration/test_cascade_integrity.py:118-156) declares -> master below --- and pins it.
    • Change: #1620 (open, closes #1619) adds the ownership rule and a warning at declaration. Raise DataJointError in the minor release after the warning. fk_attribute_map cannot decide ownership because it keeps one parent per attribute (declare.py:512-516); #1620 passes a per-reference list to the hook.
    • Compatibility: deprecation path. Only new declarations fail: a table that already exists is not declared again (schemas.py:303).
    @schema
    class Probe(dj.Entry):
        definition = """
        probe_id : int32
        """
    
        class Note(dj.Part):      # declared today; expected: DataJointError
            definition = """
            note_id : int32
            ---
            -> master
            """
    
  • 4. Delete, drop, and the cascade find a part's master by the table name, not by ownership. #1719 reports the schema-name case (a name with a non-word character, such as parts-check, hides every part), and #1728 (open) fixes the pattern. This item adds the case #1728 leaves: a part the master does not own (item 3) still matches its master by name, so the delete check refuses to delete its rows unless master rows are deleted too, and the drop check refuses to drop it before the master, although its key identifies no master row.

    • Where: extract_master (dependencies.py:36) matches the name only. Callers: table.py:1213 (delete check), table.py:1332 (drop check), diagram.py:844 and :900 (cascade from part to master), dependencies.py:64 and :77 (topo_sort). #1620 moves topo_sort to the ownership rule and leaves the delete, drop, and cascade call sites matching by name.
    • Change: after #1728 and #1620, move the delete, drop, and cascade call sites to the ownership rule that #1620 introduces.
    • Compatibility: none for owned parts. A part the master does not own stops being treated as its part by delete, drop, and the cascade, which the declaration warning in #1620 announces.
  • 5. The master-part spec contradicts the library and itself. §2.2 says "Part tables have implicit foreign key to master" (master-part.md:106); the library adds none, and the reference exists only where the definition writes it. §4.5 describes a master that "is a part of a higher-level master" (:238) and §7.4 says "Part tables cannot have their own parts" (:376). §7.1 advises against parts that "reference multiple masters" (:341), and the part in the §3.3 example references its own master and a part of another master (:161-165). §2.1 says parts "should be deleted/dropped with their master" (:86).

    • Change: edit the page in place in datajoint-docs; no library change. Compatibility: none.

Tracked elsewhere: a delete that removes some of a master's parts (#1760), a part that references a table downstream of its own master (#1619), and self.upstream reaching the parts of an ancestor master (#1481).

Two findings on parts belong to other tracking issues. contents declared on the parts of a Lookup master is ignored, or committed apart from the master's rows: #1772. Tier detection reports Part where a part has its master's tier, and no public accessor returns that tier: #1773.

Docs pages that change with these items

Page Section Change
Master-Part Relationships Specification §1.4 Master Reference State ownership: a dependency on the master, or on another part of it, in the part's primary key; any other part declaration is rejected (item 3)
§2.1 Compositional Integrity "Should be deleted" becomes "are deleted"; the sentence that calls atomicity for Manual masters a "convention" goes when item 2 ships
§2.2 Foreign Key Behavior No implicit foreign key (item 5)
§3.1 Master-First Insertion Rewrite in the transaction form of §3.2 (item 2)
§3.3 Computed Tables with Parts Part inserts outside make() are rejected (item 1)
§4.5, §7.1, §7.4 Resolve the contradictions in item 5; replace the "Avoid when" list with the criterion: one step produces the set, and downstream tables need it complete
Table Declaration Specification §8.4 Constraints "Parts must reference their master" becomes "in the primary key", and says it is checked (item 3)
Data Manipulation Specification §2.6 Auto-Populated Tables The insert guard covers parts (item 1)
§6.1 Inserting into Part Tables The example inserts master and parts as two statements; use one transaction (item 2)
§9.3 Use Transactions for Related Inserts skip_duplicates=True on the master adds parts to a master that may already exist (item 2)
AutoPopulate Specification §4.3, rule 1; §6 Part Tables Until item 1 ships, say the guard covers masters only
Relational Workflow Model "Workflow steps and table tiers"; "Master-part relationships" "Written in the same transaction" and "without application code" hold for Entry masters only after item 2; qualify until then
Data Integrity "5. Compositional integrity" Same sentence about convention as the spec §2.1 (item 2)
How-to: Insert Data "What Not to Insert" Name the parts of Compute and Ingest tables (item 1)
How-to: Master-Part Tables "When to Use Part Tables" Same criterion as the spec §7.1 (item 5)
Tutorial: Schema Design "Master-Part (Compositional Integrity)" "Inserted and deleted as an atomic unit" holds for Entry masters only after item 2

Tier renames on these pages follow #1546.

Order: the insert guard for computed parts first, the transaction rule for Entry masters last

  1. Item 1: one property and one test, released with a warning.
  2. #1728, then #1620. The schema-name fix (#1719) and item 3's warning ship with them.
  3. Item 3 raises, and item 4's delete, drop, and cascade call sites move to the ownership rule.
  4. Item 2: the warning release ships with the rewritten spec §3.1, then the raise. Before it reaches Lookup masters, the parts' contents must be inserted with the master's rows in one transaction (#1772), because decoration commits a Lookup master's rows before its parts are declared.
  5. Item 5 now; it depends on no library change.
主要語言
Python
星號
198
分支
107
平均合併
4 天 7 小時
30 天內合併 PR
12

環境準備

  • 提供 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

datajoint/datajoint-python 的其他 Issue

查看 datajoint/datajoint-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。