findAndCountAll count 得到的数字比实际返回的数据大

Jav*_*kie 3 sequelize.js

我正在使用 SequelizefindAndCountAll()作为我网站的分页。

返回findAndCountAll()的总项数( 3990)大于实际返回的数据数( 3770)。

有谁知道如何使findAndCountAll()返回的总项目数与实际返回的数据相同?

我使用的查询表findAndCountAll()包括另外两个表,如下面的代码:

const patientModel: IIncludeOptions = {
    model: Patient,
    attributes: ["deviceId", "firstName", "lastName"],
};

const deviceModel: IIncludeOptions = {
    model: Device,
    required: true,
    attributes: ["deviceId", "deviceSerialNumber"],
    include: [patientModel],
};

const eventCodeModel: IIncludeOptions = {
    model: EventCode,
    required: true,
    attributes: ["name"],
};

const opts: IFindOptions = {
    ...pageSizeLimit,
    offset : offsetNum,
    attributes: ["id", "deviceId", "eventId", "dispatchedAt", "message", "createdAt"],
    include: [
        deviceModel,
        eventCodeModel,
    ],
    order: sortBy,
};

const resCount = await DeviceEvent.findAndCountAll(opts).then((response) => {
    const totalItems = response.count;
    return {
        totalItems,
        currentPage: page || 1,
    };
});
Run Code Online (Sandbox Code Playgroud)

Vau*_*ein 7

对我有用的是添加distinct: true到查询中。如果没有这个,Sequelize 将返回没有内部连接/必需的值的计数:true。

const posts = Post.findAndCountAll({
    include: ['attachment', 'users', 'x count of models'],
    distinct: true
});
Run Code Online (Sandbox Code Playgroud)

在问题中使用的代码中,这将是:

const opts: IFindOptions = {
    ...pageSizeLimit,
    offset : offsetNum,
    attributes: ["id", "deviceId", "eventId", "dispatchedAt", "message", "createdAt"],
    include: [
        deviceModel,
        eventCodeModel,
    ],
    order: sortBy,
    distinct: true,
};
Run Code Online (Sandbox Code Playgroud)

此处提到了详细信息 - findandCountAll 中的计数问题