Sequelize findAndCountAll count错误的问题

平时做项目常用Sequelize的findAndCountAll方法,因为分页的情况下非常好用

但是今天一加了关联查询include:[xxx]之后,计算出来的count就有问题

文档上并没有说明这一点

解决方法: 需要在options中加入distinct:true

链接: https://github.com/sequelize/sequelize/issues/9481

1
2
3
4
5
6
7
let options = {
offset,
limit,
include: [projectCustomField],
distinct:true
};
let comms = await Comm.findAndCountAll(options);

这是源码中对distinct的说明

1
2
3
4
/**
* Apply COUNT(DISTINCT(col))
*/
distinct?: boolean;

Sequelize根据这个属性来过滤结果集中的重复值,但是数据库中并没有重复值,所以这应该是一个bug