A part table that references a table downstream of its own master breaks delete, drop, trace and list_tables on the connection

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

維護者通常 6 天內回覆

@dimitri-yatsenko 已經在處理了。

開始於 2026年10月17日。

  • #1620 來自 @dimitri-yatsenko —— 未關閉

評估

難度
4/5
預估耗時
3-5 天
新手友好度
25/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
停滯
技術堆疊
mysql, postgresql, python
領域
backend, database

研究方向

Start with topo_sort() in dependencies.py, then inspect its callers in Diagram._propagate_restrictions, Diagram._propagate_restrictions_upstream, diagram iteration, and Schema.list_tables(), plus the Table.declare() and Part declaration paths. Run the provided reproduction to cover cycle detection, ownership classification, declaration validation, and unaffected operations. Done means owned-part cycles are rejected with the specified DataJointError, unowned parts are warned about and sorted independently, existing-cycle operations fail clearly, and unrelated operations continue working.

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

描述

bug

Summary

DataJoint accepts a part table whose reference makes its master-part group depend on itself. After that declaration, every cascading delete, every drop, Diagram.trace() and Schema.list_tables() on the connection fail with networkx.exception.NetworkXUnfeasible. This includes operations on tables that have nothing to do with the cycle, and dropping the part that caused it.

Reproduction

import datajoint as dj

schema = dj.Schema("repro_group_cycle")


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


@schema
class Analysis(dj.Manual):
    definition = """
    -> Session
    analysis_id : int32
    """


@schema
class Subject(dj.Manual):
    definition = """
    subject_id : int32
    """


# Later, a part is added to Session that references Analysis.
class Session(dj.Manual):
    definition = """
    session_id : int32
    """

    class Summary(dj.Part):
        definition = """
        -> master
        -> Analysis
        """


schema(Session)  # succeeds: session__summary is created

Each of these then raises networkx.exception.NetworkXUnfeasible: Graph contains a cycle or graph changed during iteration:

Session.delete()
Subject.delete()                                 # Subject is unrelated to the cycle
dj.Diagram.trace(Analysis & {"session_id": 1})
schema.list_tables()
Session.Summary.drop(part_integrity="ignore")    # so the part cannot be removed through DataJoint

Observed on MySQL and on PostgreSQL with the current master.

Cause

topo_sort in dependencies.py keeps each part right after its master. To make that possible, it orders a master after every table that its parts reference. Here Session.Summary references Analysis, so Session has to come after Analysis, while Analysis references Session and has to come after it. No such order exists, and nx.topological_sort raises.

In general: treat each master and its parts as one node. The tables can be ordered exactly when these groups do not depend on each other in a cycle. Besides the case above, two masters whose parts reference each other's masters also form a cycle: A.Part references B, and B.Part references A.

Diagram.cascade(), Diagram.trace(), Diagram iteration (used by delete and drop) and Schema.list_tables() each sort the whole dependency graph loaded on the connection. That is why a single cycle breaks them for every table.

topo_sort also decides group membership by the table name alone (master__part). A part that never references its master, or references it only through a secondary or nullable foreign key, is still pulled next to its master. That causes failures in both directions:

  • m; x references m; part m__p references x but not m. Nothing here is circular, yet topo_sort raises, because it orders m after x. Even without that, keeping m__p next to m would place it before x, which it references.
  • m; part m__p1 references m only through a nullable foreign key; part m__p2 references m in its primary key and also references m__p1. topo_sort treats m__p1 as part of m's group and sorts this. But m__p1 is not tied to a master row: it is a table that depends on m, and m__p2 depends on it.

Expected behavior

  • A part belongs to its master's group only when it is owned by the master: a foreign key in the part's primary key references the master, or references another owned part of the same master. The reference may rename the key attributes. Any other part is treated as a separate table when DataJoint orders tables.
  • Declaring an owned part whose reference would close a cycle raises a DataJointError before any table is created. The message names the part, the referenced table and the cycle. An owned part may still reference its master, another owned part of the same master, or any table that does not depend on its master.
  • A cycle that already exists in a database (declared before the check) raises a DataJointError that names the cycle instead of a networkx error. Only the operations that must order the cycle raise it. Operations on other tables, and dropping the part that closes the cycle, keep working.
  • A part that is not owned gets a warning at declaration. It is sorted as a separate table, and delete and trace are expected to treat it the same way in 2.4.

Where

  • Table.declare() calls the _declare_check() hook after parsing the definition and before running any DDL. Part can override it.
  • topo_sort() in dependencies.py, and its callers: Diagram._propagate_restrictions, Diagram._propagate_restrictions_upstream, Diagram.__iter__ / __reversed__, and Schema.list_tables().
主要語言
Python
星號
198
分支
103
平均合併
6 天 1 小時
30 天內合併 PR
10

環境準備

  • 提供 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 摘要。