使用额外的列将多对多序列化

Ber*_*rdo 1 node.js sequelize.js

经过一番研究,我没有发现与我的问题相关的任何内容。所以设置是已经与 sequelize (sqllite) 一起工作的 M:M 关系:

return User.find({ where: { _id: userId } }).then(user => {
   logger.info(`UserController - found user`);
   Notification.find({ where: { _id: notificationId } }).then(notification => {
      if (associate) {
        return user.addNotification([notification]);
      } else {
        return user.removeNotification([notification]);
      }
   })
})
Run Code Online (Sandbox Code Playgroud)

问题是我在 inter 表(cityId,active)中有额外的字段,我不知道在运行“addNotification”时如何更新它。

提前致谢

小智 6

如果您使用的是Sequelize 4.x 版,API 中会有一些变化

关系添加/设置/创建设置器现在通过将属性作为 options.through 传递来设置它们(以前第二个参数被用作通过属性,现在它考虑的选项是通过作为子选项)

user.addNotification(notification, { through: {cityId: 1, active: true}});
Run Code Online (Sandbox Code Playgroud)