Flask一对多级联删除

6个月前 395次点击 来自 Flask

Flask一对多级联删除,需要在反向关系中添加cascade='all, delete-orphan'参数,实列如下:

class Person(db.Model) :
    __tablename__ = 'person'
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(32))
# 添加cascade='all, delete-orphan'参数
    books = db.relationship('Book', backref='person', lazy='dynamic',cascade='all, delete-orphan')
    
class Book(db.Model) :
    __tablename__ = 'book '
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(32))
# 外键添加ondelete='CASCADE')属性
    person_id = db.Column(db.Integer, db.ForeignKey('person.id',ondelete='CASCADE'))
Card image cap
开发者雷

尘世间一个小小的开发者,每天增加一些无聊的知识,就不会无聊了

要加油~~~

技术文档 >> 系列应用 >>
热推应用
Let'sLearnSwift
学习Swift的入门教程
PyPie
Python is as good as Pie
标签