python
data_error
ai_generated
true
实例 <Child at 0x...> 具有 NULL 标识键。如果这是自动生成的值,请检查数据库表是否允许生成新的主键值,以及映射的 Column 对象配置是否正确。
sqlalchemy.exc.FlushError: Instance <Child at 0x...> has a NULL identity key. If this is an auto-generated value, check that the database table allows generation of new primary key values, and that the mapped Column object is configured correctly.
ID: python/sqlalchemy-relationship-delete-orphan-without-cascade
80%修复率
84%置信度
0证据数
2025-01-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
试图删除未正确关联或缺少主键的子对象,通常由于缺少 cascade delete-orphan。
English
Trying to delete a child object that is not properly associated or has missing primary key, often due to missing cascade delete-orphan.
解决方案
-
95% 成功率
class Parent(Base): children = relationship('Child', cascade='all, delete-orphan') # Then remove child: parent.children.remove(child) -
80% 成功率
session.delete(child) session.commit()
无效尝试
常见但无效的做法:
-
50% 失败
May conflict with auto-increment; not scalable.
-
70% 失败
The child still exists in session and gets flushed.