我試圖爲我的應用程序設置一個DELETE路線,它使用Sequelize和Postgres數據庫。我有兩種不同的模式,「產品」和「興趣」(將興趣視爲某種類別或標籤)。產品可以有多個興趣和興趣可以有多個產品,所以我正在使用belongsToMany
關聯。因此,我有三個表格:Products
,Interests
和ProductInterests
。下面是我如何的關聯設置:Sequelize Model - 刪除項目與belongsToMany n:m協會
關於產品:
Product.belongsToMany(models.Interest, {
foreignKey: 'productId',
through: models.ProductInterest,
as: 'interests',
onDelete: 'CASCADE',
hooks: true
});
對於興趣愛好:
Interest.belongsToMany(models.Product, {
through: models.ProductInterest,
as: 'products',
foreignKey: 'interestId',
onDelete: 'CASCADE',
hooks: true
});
而且這是被稱爲在我的控制器產品的方法:
destroy(req, res) {
return Product
.findById(req.params.productId, {
include: [
{
association: Product.associations.interests
}
]
})
.then((product) => {
if (!product) {
return res.status(404).send({
message: 'Product not found'
});
}
return product
.destroy()
// I've also tried the following:
// .destroy({
// truncate: true,
// cascade: true
// })
.then(() => res.status(204).send())
.catch(error => res.status(400).send(error));
})
.catch(error => res.status(400).send(error));
這會導致無限循環,並記錄下這些查詢:
Executing (default): SELECT "Product"."id", "Product"."title", "Product"."brand", "Product"."url", "Product"."imageUrl", "Product"."currentPrice", "Product"."male", "Product"."female", "Product"."asin", "Product"."storeName", "Product"."createdAt", "Product"."updatedAt", "ProductInterest"."createdAt" AS "ProductInterest.createdAt", "ProductInterest"."updatedAt" AS "ProductInterest.updatedAt", "ProductInterest"."interestId" AS "ProductInterest.interestId", "ProductInterest"."productId" AS "ProductInterest.productId" FROM "Products" AS "Product" INNER JOIN "ProductInterests" AS "ProductInterest" ON "Product"."id" = "ProductInterest"."productId" AND "ProductInterest"."interestId" = 1;
Executing (default): SELECT "Interest"."id", "Interest"."name", "Interest"."createdAt", "Interest"."updatedAt", "ProductInterest"."createdAt" AS "ProductInterest.createdAt", "ProductInterest"."updatedAt" AS "ProductInterest.updatedAt", "ProductInterest"."interestId" AS "ProductInterest.interestId", "ProductInterest"."productId" AS "ProductInterest.productId" FROM "Interests" AS "Interest" INNER JOIN "ProductInterests" AS "ProductInterest" ON "Interest"."id" = "ProductInterest"."interestId" AND "ProductInterest"."productId" = 6;
我的意圖是:a)刪除Product表中的產品; b)刪除ProductInterest表中的關聯記錄。如果產品被刪除,我不希望刪除興趣本身。同樣,如果刪除興趣(僅關聯),我不希望刪除關聯的產品。
我已經嘗試過幾種不同的排列,並且找不到正確的方法。任何幫助,將不勝感激。
UPDATE
調試sequelize代碼一點使我覺得我不應該使用cascade
這樣。它基本上試圖刪除產品,然後(由於級聯)試圖刪除興趣(我不想),哪些(由於級聯),然後嘗試刪除相關的產品等等等等。仍然不確定什麼是正確的解決方案是