我有一个像这样设置的协会:
m.User.hasMany(m.Interests, { joinTableName: 'user_interests', foreignKey: 'user_id' });
m.Interests.hasMany(m.User, { joinTableName: 'user_interests', foreignKey: 'interest_id' });
Sequelize 很棒,因为我可以做 user.getInterests。但是我怎样才能添加一个新的关联呢?
最佳答案
您可以执行以下任一操作:
user.addInterest(interest)
要添加新兴趣,而不更改当前兴趣,或者您可以执行以下操作:
user.setInterests([interest])
第二个选项意味着仅将我刚刚给您的兴趣与用户相关联,并删除所有旧的关联(如果存在)。
https://sequelize.readthedocs.io/en/v3/docs/associations/
关于sequelize.js - Sequelize 添加关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18828218/