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

huangapple go评论139阅读模式
英文:

Sequelize findAndCountAll returns wrong count when association models included

问题

I have models like picture below:

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

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:

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

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',

huangapple
  • 本文由 发表于 2023年6月1日 14:17:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定