Sequelize : Sum with include - TypeError: Cannot read properties of undefined (reading 'fn')

Sequelize : Sum with include - TypeError: Cannot read properties of undefined (reading 'fn')

我想像 SQL 那样在查询中求和:

SELECT `coproprietaires`.`id`,
       `coproprietaires`.`name`,
       `coproprietaires`.`email`,
       `LotsOwner`.`lotId` AS `lotId`,
       `LotsOwner`.`distributionKeyId` AS `costKey`,
       SUM(`LotsOwner`.`tantiemeNumber`) AS `tantiemeNumber`
FROM `coproprietaires` AS `coproprietaires`
LEFT OUTER JOIN `lotstantiemes` AS `LotsOwner` ON `coproprietaires`.`id` = `LotsOwner`.`ownerId`
WHERE `coproprietaires`.`coproNumber` = '85656913'
GROUP BY `name`,
         `LotsOwner`.`distributionKeyId`
ORDER BY `coproprietaires`.`name` ASC;

当我写成Sequelize格式时,它不起作用:

const data = await modelOwners.findAll({
  attributes: ["id", "name", "email"],
  include: [
    {
      model: modelLotsTantiemes,
      as: "LotsOwner",
      attributes: [
        "lotId",
        ["distributionKeyId", 'costKey'],
        [ sequelize.fn('sum', sequelize.col("tantiemeNumber")), 'totalTantieme' ],
      ],
    },
  ],
  where: { coproNumber: req.header("customerId") },
  group: ["name", "LotsOwner.distributionKeyId"],
  raw: true,
  order: [["name", "ASC"]],
});

错误是:

TypeError: Cannot read properties of undefined (reading 'fn')

有人可以帮助我吗?请

感谢

您应该调用 fncol 作为 Sequelize 的静态成员和 sequelize 的实例成员(存储连接):

const Sequelize = require('sequelize')
...
[ Sequelize.fn('sum', Sequelize.col("tantiemeNumber")), 'totalTantieme' ]