python - SQLAlchemy 级联删除-孤儿不删除孤儿
问题描述
我有一个关系类似的 SQLAlchemy 模型
class Parent:
children = relationship("Child", back_populates="parent", passive_deletes=True, cascade="all, delete-orphan")
class Child:
parent_id = Column(
Integer,
ForeignKey(
"parent.id",
deferrable=True,
initially="IMMEDIATE",
ondelete="NO ACTION",
),
nullable=False,
)
parent = relationship("Parent", back_populates="child")
但我发现数据并不总是孤立的。例如:
p = Parent()
p.children = [Child(parent=p))]
db.session.commit()
p.children = [Child(parent=p)]
print('before commit', p.children)
db.session.commit()
print('after commit', p.children)
输出
before commit [Child<transient>]
after commit [Child<1>, Child<2>]
根据我读过的文档,我希望这样做p.children = [Child]
会孤立前一个p.children
(然后让他们通过 删除cascade="all, delete-orphan"
),但这似乎不会在这里发生。
有没有人对可能发生的事情有任何想法?
解决方案
推荐阅读
- swift - NavigationBar 覆盖 tabBarController 中的其他视图控制器
- python - Pandas:基于函数有效更新列值
- mongodb - Mongo 为集合中的每个数组项更新一个数组属性
- angularjs - Angular/Ionic 代码调用函数尚未加载
- javascript - 我想将输入字段中的数据添加到从构造函数创建的新对象并将其输出到浏览器中?
- nuget - Azure 管道不会创建预发布版本
- java - 如何使用 ArrayList 在 Java Jtable 中显示 mysql 表中的所有数据?
- javascript - Webpack 文件加载器未加载图像,404 错误
- r - R 在 Azure Pipeline 中找不到环境变量
- python - 让 Python 输入与小写和大写输入一起工作的问题