Home > Enterprise >  sequelize findone adding extra condition in query
sequelize findone adding extra condition in query

Time:10-11

I have node js with sequelize and mysql . I have used following syntax to find one record

 await SkillPlanUser.findOne({
        where: { deleted_at: {
          [Op.ne]: null
        },skill_plan_id: skill_plan_id, user_id: val },
      })

But its executing like below:

SELECT `id`,
       `skill_plan_id`,
       `user_id`,
       `created_at`,
       `updated_at`,
       `deleted_at`
FROM `skill_plan_user` AS `skill_plan_user`
WHERE (`skill_plan_user`.`deleted_at` IS NULL
       AND (`skill_plan_user`.`deleted_at` IS NOT NULL
            AND `skill_plan_user`.`skill_plan_id` = '3'
            AND `skill_plan_user`.`user_id` = '5'))
LIMIT 1;

In above query skill_plan_user.deleted_at IS NULL AND` ...gets added even its not mentioned.

I am not getting why its get added and how I can remove that?

Please guide.

CodePudding user response:

It seems like you indicated paranoid: true in the model options (that means you use soft-delete feature).
When the paranoid mode is turned on for the model then by default you will get all non-deleted records so there is no need to indicate this condition:

deleted_at: {
          [Op.ne]: null
        }

Sequelize will do it itself (as you already found out). If you want to get all records including deleted ones you need to indicate paranoid: false in findOne as Emma already mentioned in the comment.

await SkillPlanUser.findOne({
        where: { skill_plan_id: skill_plan_id, user_id: val },
      }, { paranoid: false })
  • Related