【问题标题】:Sequelize Many to Many - Updating associations tableSequelize Many to Many - 更新关联表
【发布时间】:2020-07-10 20:48:12
【问题描述】:
我正在使用带有 Node.js 的 Sequelize 构建一个应用程序,其中我有一个 Product 模型和一个 Category 模型。
一个产品可以有多个类别,一个类别可以有多个产品。
我想更新关联(添加和删除产品类别)
如何更新关联表?
db.Product.find({
where: {
id: 1
}
}).then((res) => {
// Updating associations table HERE
})
【问题讨论】:
标签:
javascript
node.js
postgresql
sequelize.js
【解决方案1】:
这里我假设您有这样的关联定义:
Product.belongsToMany(Category,
{ through: ProductCategory, foreignKey: 'productId', otherKey: 'categoryId' })
// Updating associations table HERE
res.addCategories(categories).then(x => {
// categories added successfully to the product
})