Sequelize:将两列关联为一个table。添加关联后,插入行时出现错误

Sequelize: Associating two columns in one table. After i added my associations i am getting an error when inserting a row

提前致谢!

所以每次我尝试向 table 中插入新行时都会收到错误消息。在我添加我的关联之前,我测试了我的 table 和我在邮递员中的创建功能,并且能够成功插入 table。只有在我添加关联后问题才会出现。这是我的 table 和关联

id | userOneId | userTwoId | status | actionUserId |         createdAt          |         updatedAt          

这是协会

  models.relationship.belongsTo(models.user,  {as:"userOne", foreignKey:"userOneId"})
  models.relationship.belongsTo(models.user,  {as:"userTwo", foreignKey:"userTwoId"})
  models.user.hasMany(models.relationship, {as:"userOne", as:"userTwo"})

一旦我每次尝试插入新行时都添加了关联,我就会从数据库中收到此消息,然后在最后出现错误

Executing (default): INSERT INTO "relationships" ("id","userOneId","userTwoId","status","createdAt","updatedAt") VALUES (DEFAULT,,,,,) RETURNING "id","userOneId","userTwoId","status","actionUserId","createdAt","updatedAt","userId"; DatabaseError [SequelizeDatabaseError]: column "userId" does not exist

我的 catch 语句也提示

code: '42703',
hint: 'Perhaps you meant to reference the column "relationships.userOneId" or the column "relationships.userTwoId".',

因此,通过深入研究 Sequelize 文档,我发现了问题所在。必须将外键从关系翻转到用户

models.relationship.belongsTo(models.user, {as:"userOne",}) models.relationship.belongsTo(models.user, {as:"userTwo",})

  models.user.hasMany(models.relationship, {foreignKey:"userOneId", foreignKey:"userTwoId"})