英文:
Sequelize findAndCountAll returns wrong count when association models included
问题
I have models like picture below:
and the relations are defined as:
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)
now when I include DoctorService or DoctorSpecializtion, find and count all returns the wrong number.
I also tried separate: true
include: [
{
separate: true,
model: DoctorSpecializtion,
attributes: ['id'],
where: { specializationId: { [Op.or]: value } },
include: [
{
model: Specialization,
attributes: ['title', 'slug'],
},
{
model: Level,
attributes: ['title', 'slug'],
},
],
},
],
but when I use separate true, the filtering part which is
where: { specializationId: { [Op.or]: value } },
doesn't return my required result.
英文:
I have models like picture below:
and the relations are defined as :
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)
now when i include DoctorService or DoctorSpecializtion, find and count all returns wrong number.
I also tried 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'],
},
],
},
],
but when i use seperate true the filting part which is
where: { specializationId: { [Op.or]: value } },
it doest return my required result.
答案1
得分: 0
I fixed this by removing seperate: true
from my queries and added this query to my findAndCountAll
query:
distinct: true,
col: 'Doctor.id',
英文:
i fixed this by removing seperate:true from my queries and added this query to my findAndCountAll query
distinct: true,
col: 'Doctor.id',
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论