sqlalchemy 联级删除

class MyClass(Base):
    __tablename__ = 'mytable'
    id = Column(Integer, primary_key=True)
    children = relationship("MyOtherClass",
                    cascade="all, delete-orphan",
                    passive_deletes=True)

class MyOtherClass(Base):
    __tablename__ = 'myothertable'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer,
                ForeignKey('mytable.id', ondelete='CASCADE')
                    )

relationship关联时要加上passive_deletes=True 

外键要加上ondelete='CASCADE'

否则sqlalchemy不能联级删除

你可能感兴趣的:(mysql,python)