如果子表中的关联行已被删除,我想删除父行。
class Child(Base):
__tablename__ = "children"
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey("parents.id", ondelete='CASCADE'))
class Parent(Base):
__tablename__ = "parents"
id = Column(Integer, primary_key=True)
child = relationship(Child, backref="parent", passive_deletes=True)
child_obj = session.query(Child).first()
session.delete(child_obj)
session.commit()
它确实删除了子对象,但父对象保持原样。我想删除父级以及使用层叠。