1.模型 代码
post_tags = db.Table('post_tags',
db.Column('post_id', db.Integer, db.ForeignKey('posts.id',ondelete='CASCADE'),
primary_key=True),
db.Column('tag_id', db.Integer, db.ForeignKey('tags.id',ondelete='CASCADE'), primary_key=True)
)
class Posts(db.Model):
__tablename__ = "posts"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(40))
bookstyles = db.relationship("Tags", secondary=post_tags, backref=db.backref("posts", lazy="dynamic"),
lazy="dynamic",cascade="all, delete-orphan",single_parent=True) # 多 对 多 # cascade="all, delete-orphan",passive_deletes=True
class Tags(db.Model):
__tablename__ = "tags"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(40))
post = Posts.query.get(1)
db.session.delete(post) # how to auto delete `post_tags`record,but dont delete `tags`record.
db.session.commit()
2.如何删除一个post 同时auto delete 他相关的post_tags记录。但不删除tags?
3.应该如何来配置?
我发现什么都不加 就是理想状态。