Sequelize 在多对多关系中求和

Sequelize find sum in Many to Many Relationship

我需要在实体 Product 和 Store(Wharehouse) 之间建立多对多关系。 所以我就这样和Sequlize建立了关系

Product.belongsToMany(Store, { through: 'product_store' });
Store.belongsToMany(Product, { through: 'product_store' });

product_store 是与 Store 和 Product 保持关系的 table。在商店中保存产品时需要一些额外的详细信息,例如 quantity 所以我在 product_store[=16= 中添加了一个字段]

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

我通过以下方式成功保存了商店中的产品

await store.addProduct(product, {
                through: {
                    uid: "3462",
                    ws_price: 100,
                    discount: 2,
                    bonus: 1,
                    quantity: 2000,
                    total: 200000,
                }
            })

值已正确保存,我可以获得 store.getProdcuts() 的产品。但是现在我需要查询所有商店的产品及其数量总和。我已经开始用

查询它
let products = await Product.findAll({
                include: {
                    model: Store,
                    through: { attributes: ["ws_price", "discount", "bonus", "quantity", "total"] }
                }
            })

像这样查询带有商店详细信息(包括数量)的产品

[
  {
    "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"
        }
      }
    ]
  }
]

我只需要获取所有店铺的商品详情和商品总和,如何实现?。我不熟悉将 Count/Sum 用于关系。提前致谢!

最正确的方法是通过 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']
      ]
   }
})