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

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

  •  
  •   coolair · 2019-01-15 14:04:25 +08:00 · 466 次点击
    这是一个创建于 1928 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如:

    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   ·   我们的愿景   ·   实用小工具   ·   2891 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 03:16 · PVG 11:16 · LAX 20:16 · JFK 23:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.