Sequelize:发现循环依赖

时间:2017-08-07 23:27:32

标签: node.js postgresql sequelize.js

依赖链:关系 - > users =>关系

目标是让Relationships拥有user_one,user_two和last_user操作。标准的user_one,user_two关系工作正常,但是当我尝试使用Relationships.hasOne()添加last_user_action时,我遇到了依赖链错误。对此有快速解决方法吗?

var Relationships = sequelize.define(
"Relationships",
{
  type: DataTypes.ENUM('block', 'follow', 'pending')
},
{ 
  classMethods: {
    associate: function(models) {
      Relationships.hasOne(models.Users, { as: 'lastUserAction', foreignKey: 'last_user_action' });
    }
  },
  tableName: "relationships"
}
);


var Users = sequelize.define(
"Users",
{
  firstName: DataTypes.STRING
},
{
  classMethods: {
    associate: function(models) {
      Users.belongsToMany(models.Users, { as: 'userRelationship', through: models.Relationships, foreignKey: 'user_one_id', otherKey: 'user_two_id' });
    }
  },
  tableName: "users"
}
);

1 个答案:

答案 0 :(得分:1)

Users.hasMany(models.Relationships, { as: 'lastUserAction', foreignKey: 'last_user_action' });

而不是Relationships.hasOne()