from app import db
domains_accounts_relation = db.Table('domains_accounts_relation',
db.Column('domain_id', db.String(8),
db.ForeignKey('domain_domains.id', ondelete='CASCADE'),
primary_key=True),
db.Column('notice_user_id', db.Integer,
db.ForeignKey('account_accounts.id', ondelete='CASCADE'),
primary_key=True)
)
class Domains(db.Model):
__tablename__ = 'domain_domains'
id = db.Column(db.String(8), primary_key=True)
name = db.Column(db.String(64))
creation_date = db.Column(db.DateTime)
registry_expiry_date = db.Column(db.DateTime)
expiration_notice = db.Column(db.Boolean)
notice_users = db.relationship('Account',
secondary=domains_accounts_relation,
backref=db.backref('notice_user_domains', lazy='dynamic'),
lazy='dynamic',
cascade='all, delete',
passive_deletes=True
)
relationship中设置cascade='all, delete-orphan' 与 passive_deletes = True,
ForeignKey中设置ondelete = 'CASCADE'。
即可完成删除User的同时,删除与该user对象相关的task对象。