sequelize.js - findAndCountAll 计数得到一个比实际返回数据更大的数字

标签 sequelize.js

我将 Sequelize findAndCountAll() 用于我网站的分页。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,
    };
});

最佳答案

尝试检查您的 SQL 记录查询。
按照这些说明获取查询转储:How can I see the SQL generated by Sequelize.js?
并查看计数数字是否与 findAndCountAll() 返回给您的相同。
也许 sequelize.js 挂载查询的方式会影响结果。

关于sequelize.js - findAndCountAll 计数得到一个比实际返回数据更大的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64354987/

相关文章:

node.js - 序列化嵌套数据结构

mysql - 无法使用 Sequelize 库连接三个具有 Sum 聚合函数的表

postgresql - 对 sequelize seeder 的自定义查询

postgresql - Sequelize.JS 按日期返回下一个最早的条目?

mysql - 如何防止sequelizer删除数据库

sequelize.js - Sequelize 查询 - 比较两列中的日期

node.js - 在每个 GET 的 Sequelize 中自动包含引用模型

node.js - 如何返回刚刚创建的行及其与 sequelize 的关联

node.js - 仅从 Sequelize 结点模型返回指定的关联模型

node.js - sequelize 未在 index.js 中为 sequelize 模型定义