sqlalchemy里面many----one关联的级联删除设置如何解决?
sqlalchemy里面many----one关联的级联删除设置如何解决?
(many) (one)
Student Klass
class Student(Base):
tablename
= 'student'
id = Column(Integer, primary_key=True)
name = Column(String(255))
klass_id = Column(Integer, ForeignKey('klass.id'), nullable=False)
klass = relationship('Klass', backref=backref('students', cascade="all, delete-orphan"))
class Klass(Base):
tablename
= 'klass'
id = Column(Integer, primary_key=True)
name = Column(String(255), nullable=False)
要求:
删除student端数据A的时候,如果student是唯一的和klass关联的对象,那么同时删除klass对象。
参考文档: http://docs.sqlalchemy.org/en/rel_0_9/orm/session.html#cascades --- 没有弄明白,小白呀
芹沢文乃_
10 years, 2 months ago