[英]Sequelize Many to Many - Updating associations table
我正在使用帶有 Node.js 的 Sequelize 構建一個應用程序,其中我有一個產品 model 和一個類別 model。
一個產品可以有很多類別,一個類別可以有很多產品。 我想更新關聯(添加和刪除產品的類別)如何更新關聯表?
db.Product.find({
where: {
id: 1
}
}).then((res) => {
// Updating associations table HERE
})
在這里,我假設您有這樣的關聯定義:
Product.belongsToMany(Category,
{ through: ProductCategory, foreignKey: 'productId', otherKey: 'categoryId' })
// Updating associations table HERE
res.addCategories(categories).then(x => {
// categories added successfully to the product
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.