续集:包含的总和 - 类型错误:无法读取未定义的属性(读取"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')

有人能帮帮我吗?请。

Thx

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

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

最新更新