Cyclic dependency found. Users is dependent of itself,mysql表循环依赖问题

冲突关联:

Cyclic dependency found. Users is dependent of itself

// A关联了B,B关联了A

遇到的场景:

用户表关联角色,角色关联用户(表示是哪个用户创建的)

sequelize创建表时候遇到的问题。

解决方式:

 # 这个必须加,sequelize要建立关联关系才能连表,但是这个关系又不能作用到表结构上,就加,constraints: false

app.model.Role.belongsTo(app.model.User, {
      foreignKey: "user_id",
      // 循环依赖解决方式,不创建关联
      constraints: false, allowNull:true, defaultValue:null
    })

参考文档:

https://www.thinbug.com/q/35116541

原文地址:https://www.cnblogs.com/zezhou/p/15129347.html