NodeJS Sequelize findAndCountAll在包含关联模型时返回错误计数

w8biq8rn  于 2023-06-05  发布在  Node.js
关注(0)|答案(1)|浏览(218)

我有像下面的图片模型:

并且这些关系被定义为:

Level.hasMany(DoctorSpecializtion);
DoctorSpecializtion.belongsTo(Level);

DoctorSpecializtion.belongsTo(Specialization)
Doctor.hasMany(DoctorSpecializtion)
DoctorSpecializtion.belongsTo(Doctor)

Service.belongsToMany(Doctor, { through: DoctorService })
Doctor.belongsToMany(Service, { through: DoctorService })
Service.hasMany(DoctorService)
DoctorService.belongsTo(Service)
Doctor.hasMany(DoctorService)
DoctorService.belongsTo(Doctor)

现在,当我包括DoctorService或DoctorSpecializtion时,查找和计数所有返回错误的数字。我也试过separete:true

include: [
                        {
                             separate: true,
                           
                            model: DoctorSpecializtion,
                            

                            attributes: ['id'],
                            where: { specializationId: { [Op.or]: value } },
                            include: [
                                {
                                    model: Specialization,
                                    attributes: ['title', 'slug'],
                                },
                                {
                                    model: Level,
                                    attributes: ['title', 'slug'],
                                },
                            ],
                        },
                    ],

但是当我使用separate true的过滤部分是

where: { specializationId: { [Op.or]: value } },

它确实返回了我需要结果。

tmb3ates

tmb3ates1#

我通过从我的查询中删除separate:true并将此查询添加到我的findAndCountAll查询中来修复此问题

distinct: true,
col: 'Doctor.id',

相关问题