Bernardo Bernardo - 2 months ago 19
Node.js Question

Sequelize many to many with extra columns

After some research I didn't find anything related to my problem. So the setting is an M:M relationship already working with sequelize (sqllite):

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]);
}
})
})


The thing is that I have extra fields in the inter table(cityId, active) and I don't know how to update it when running "addNotification".

Thanks in advance

Answer

In order to add data to pivot table you should pass data as second parameter of add function

user.addNotification(notification, {cityId: 1, active: true});
Comments