Sqlalchemy 的级联删除配置

2018-11-12 16:36:30 +08:00
 fanhaipeng0403

class BaseModel(Model):
    id = Column(Integer, primary_key=True)
    created_at = Column(DateTime(True), default=func.now(), nullable=False)
    updated_at = Column(DateTime(True), default=func.now(), onupdate=func.now(), nullable=False)

    @classmethod
    def create(cls, **kw):
        session = db.session
        if 'id' in kw:
            obj = session.query(cls).get(kw['id'])
            if obj:
                return obj
        obj = cls(**kw)
        session.add(obj)
        session.commit()
        return obj

    def to_dict(self):
        columns = self.__table__.columns.keys()
        return {key: getattr(self, key) for key in columns}


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///../cnblogblog.db'

db = SQLAlchemy(app, model_class=BaseModel)


######################################################################################################

class Parent(db.Model):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    name = Column(String(20))


class Child(db.Model):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    name = Column(String(20))

    parent_id = Column(Integer, ForeignKey('parent.id'))
    parent = relationship("Parent", backref=backref("child"))

    ###  只删除父级,子不影响
    # 1. parent_id = Column(Integer, ForeignKey('parent.id', ondelete="CASCADE"))
    #    parent = relationship("Parent", backref=backref("child", passive_deletes=True))

    ###  子级跟随删除
    # 2. parent = relationship("Parent", backref = backref("child", cascade="all, delete-orphan"))
    # 3. parent = relationship("Parent", backref = backref("child", cascade="all,delete"))

    ##  父级删除,子级不删除,外键更新为 null
    # 4. parent = relationship("Parent", backref = backref("child"))


if __name__ == '__main__':
    db.create_all()
    Parent.create(name='ZhangTian')
    Parent.create(name='LiTian')
    Child.create(name='ZhangDi', parent_id=1)
    Child.create(name='LiDi', parent_id=2)

    parent = db.session.query(Parent).first()
    db.session.delete(parent)
    db.session.commit()
    ```
1770 次点击
所在节点    Python
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/507065

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX