[英]Sequelize find sum in Many to Many Relationship
I need to build a many-to-many relationship between entities Product and Store(Wharehouse).我需要在实体产品和商店(仓库)之间建立多对多的关系。 So I managed to build a relationship with Sequlize like this.所以我设法像这样与 Sequlize 建立了关系。
Product.belongsToMany(Store, { through: 'product_store' });
Store.belongsToMany(Product, { through: 'product_store' });
product_store is the table that holds the relationship with Store and Product. product_store是保存与 Store 和 Product 关系的表。 When saving a product in a store in need some additional details like quantity so I added a field to the product_store当在商店中保存产品时需要一些额外的细节,比如数量,所以我在product_store添加了一个字段
const Product_Store = sequelize.define("product_store", {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true,
},
uid: {
type: STRING,
allowNull: false,
unique: true,
},
ws_price: {
type: DECIMAL(10, 2),
},
discount: {
type: DECIMAL(10, 2),
},
bonus: {
type: DECIMAL(10, 2),
},
quantity: {
type: DECIMAL(10, 2),
},
total: {
type: DECIMAL(10, 2),
},
active: {
type: BOOLEAN,
defaultValue: true,
},
createdAt: {
type: DATE,
},
updatedAt: { type: DATE },
});
I managed to save the product in the store in the following way我设法通过以下方式将产品保存在商店中
await store.addProduct(product, {
through: {
uid: "3462",
ws_price: 100,
discount: 2,
bonus: 1,
quantity: 2000,
total: 200000,
}
})
The values are saved properly and I could get products with store.getProdcuts()
.这些值已正确保存,我可以使用store.getProdcuts()
获得产品。 But now I need to query the products and the sum of their quantities in all stores.但是现在我需要查询所有商店的产品及其数量的总和。 I've started to query it with我已经开始查询它
let products = await Product.findAll({
include: {
model: Store,
through: { attributes: ["ws_price", "discount", "bonus", "quantity", "total"] }
}
})
Which queries the product with store details(including the quantity) like this像这样使用商店详细信息(包括数量)查询产品
[
{
"id": 1,
"uid": "4323",
"name": "Sunlight",
"active": true,
"createdAt": "2022-01-24T06:38:47.000Z",
"updatedAt": "2022-01-24T06:38:47.000Z",
"supplierId": null,
"typeId": null,
"unitId": null,
"stores": [
{
"id": 1,
"uid": "122333",
"name": "Store1",
"active": true,
"createdAt": "2022-01-24T06:38:47.000Z",
"updatedAt": "2022-01-24T06:38:47.000Z",
"product_store": {
"ws_price": "150.00",
"discount": "2.00",
"bonus": "1.00",
"quantity": "2000.00",
"total": "300000.00"
}
},
{
"id": 2,
"uid": "34523",
"name": "Store2",
"active": true,
"createdAt": "2022-01-24T06:38:47.000Z",
"updatedAt": "2022-01-24T06:38:47.000Z",
"product_store": {
"ws_price": "300.00",
"discount": "2.00",
"bonus": "1.00",
"quantity": "3000.00",
"total": "300000.00"
}
}
]
}
]
I only need to get the product details and the sum of product in all stores how can I achieve this?.我只需要获取产品详细信息和所有商店的产品总和如何实现? I'm not familiar with using Count/Sum with relationships.我不熟悉在关系中使用 Count/Sum。 Thanks in advance!提前致谢!
The most correct way is to use a subquery via Sequelize.literal
like this:最正确的方法是通过Sequelize.literal
使用子查询,如下所示:
let products = await Product.findAll({
attributes: {
include: [
[Sequelize.literal('(SELECT sum(product_store.quantity) FROM product_store WHERE product_store.product_id=product.id)'), 'product_quantity']
]
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.