DataJoint:删除表时发生完整性错误

问题描述

我正在设计我们的数据库,并在 Session 架构中有一个名为 common_exp 的表,其定义如下:

@schema
class Session(dj.Manual):
    deFinition = """ # information about the session and experimental setup
    -> common_mice.Mouse
    day             : date           # Date of the experimental session (YYYY-MM-DD)
    trial           : tinyint        # Counter of experimental sessions on the same day (base 1)
    ---
    id              : varchar(128)   # Unique identifier
    path            : varchar(256)   # Relative path of this session on the server
    counter         : smallint       # Overall counter of all sessions across mice (base 0)
    experimenter    : varchar(128)   # Who actually performed the experiment,must be a username from Investigator
    -> Anesthesia
    -> Setup
    -> Task
    notes           : varchar(2048)  # description of important things that happened
    """

我想更改某些属性名称,因此想删除该表。但是,我遇到了这个错误

common_exp.Session().drop()
`common_exp`.`session` (0 tuples)
Proceed? [yes,No]: >? yes

Traceback (most recent call last):
  File "C:\Anaconda3\envs\datajoint_wahl\lib\site-packages\IPython\core\interactiveshell.py",line 3441,in run_code
    exec(code_obj,self.user_global_ns,self.user_ns)
  File "<ipython-input-4-bce682713228>",line 1,in <module>
    common_exp.Session().drop()
  File "C:\Anaconda3\envs\datajoint_wahl\lib\site-packages\datajoint\table.py",line 474,in drop
    FreeTable(self.connection,table).drop_quick()
  File "C:\Anaconda3\envs\datajoint_wahl\lib\site-packages\datajoint\table.py",line 450,in drop_quick
    self.connection.query(query)
  File "C:\Anaconda3\envs\datajoint_wahl\lib\site-packages\datajoint\connection.py",line 302,in query
    self._execute_query(cursor,query,args,suppress_warnings)
  File "C:\Anaconda3\envs\datajoint_wahl\lib\site-packages\datajoint\connection.py",line 268,in _execute_query
    raise translate_query_error(err,query)
  File "C:\Anaconda3\envs\datajoint_wahl\lib\site-packages\datajoint\connection.py",line 266,in _execute_query
    cursor.execute(query,args)
  File "C:\Anaconda3\envs\datajoint_wahl\lib\site-packages\pyMysqL\cursors.py",line 148,in execute
    result = self._query(query)
  File "C:\Anaconda3\envs\datajoint_wahl\lib\site-packages\pyMysqL\cursors.py",line 310,in _query
    conn.query(q)
  File "C:\Anaconda3\envs\datajoint_wahl\lib\site-packages\pyMysqL\connections.py",line 548,in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "C:\Anaconda3\envs\datajoint_wahl\lib\site-packages\pyMysqL\connections.py",line 775,in _read_query_result
    result.read()
  File "C:\Anaconda3\envs\datajoint_wahl\lib\site-packages\pyMysqL\connections.py",line 1156,in read
    first_packet = self.connection._read_packet()
  File "C:\Anaconda3\envs\datajoint_wahl\lib\site-packages\pyMysqL\connections.py",line 725,in _read_packet
    packet.raise_for_error()
  File "C:\Anaconda3\envs\datajoint_wahl\lib\site-packages\pyMysqL\protocol.py",line 221,in raise_for_error
    err.raise_MysqL_exception(self._data)
  File "C:\Anaconda3\envs\datajoint_wahl\lib\site-packages\pyMysqL\err.py",line 143,in raise_MysqL_exception
    raise errorclass(errno,errval)
pyMysqL.err.IntegrityError: (1217,'Cannot delete or update a parent row: a foreign key constraint fails')

如您所见,该表是空的,并且没有进一步的依赖项。错误消息也没有告诉我是哪个键造成了问题,或者哪个其他表,所以我有点困惑问题可能出在哪里。

我使用 root 帐户访问数据库,所以权限应该不是问题。从其他模式中删除表是有效的,只是这个模式会产生这个错误

解决方法

此错误表示在可以删除 common_exp.Session 之前需要删除 Session 下游的依赖表。

通常,DataJoint 将级联并删除所有下游表。但是,有时 DataJoint 看不到依赖表,因为它们位于尚未加载的不同架构中。您将需要加载进行此引用的架构,以允许 DataJoint 级联放置。您可以使用 dj.list_schemas() 查看所有可用的架构。

,

感谢您的提问-

基于表中没有记录但删除失败并出现此错误这一事实,似乎确实存在使用外键定义的下游表返回会话表,因此删除会话表会留下这些表“孤立”,从而造成完整性错误。从示例中并不清楚情况并非如此。

以下 SQL 查询在架构级别检查正向/反向依赖关系(回答问题:“哪个架构取决于哪些其他架构”):

    SELECT distinct(UNIQUE_CONSTRAINT_SCHEMA)
    FROM information_schema.REFERENTIAL_CONSTRAINTS
    where constraint_schema='my_schema'; -- forward dependencies for 'my_schema'

    SELECT distinct(CONSTRAINT_SCHEMA)
    FROM information_schema.REFERENTIAL_CONSTRAINTS
    where unique_constraint_schema='my_schema'; -- reverse dependencies for 'my_schema'

不幸的是,我不记得确切的列,但信息架构中也提供了表级信息。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...