V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
coolair
V2EX  ›  问与答

请问一下 SQLAlchemy 下的 many-to-many 如何防止删除有 relationship 的元素?

  •  
  •   coolair · 2019 年 1 月 15 日 · 698 次点击
    这是一个创建于 2648 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如:

    class User(Model):
        roles = relationship('Role', secondary='users_roles', backref=db.backref('users'))
    
    users_roles = db.Table(
        'users_roles',
        reference_col('users', ondelete='CASCADE', name='user_id'),
        reference_col('roles', ondelete='CASCADE', name='role_id')
    )
    

    有没有办法在删除 role 的时候检测是否有 user 关联,有的话就报异常,无法删除。

    目前尚无回复
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   1634 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 16:32 · PVG 00:32 · LAX 09:32 · JFK 12:32
    ♥ Do have faith in what you're doing.