【问题标题】:How to add AND condition when use inner join on Sequelize在 Sequelize 上使用内连接时如何添加 AND 条件
【发布时间】:2021-12-16 10:14:15
【问题描述】:

我有一些这样的 sql 代码

select *
from people_work_attendances pwa
inner join people_work_placements pwp on pwa.user_work_id = pwp.id
inner join company_job_profile_r_attendance cjpa on pwp.job_profile = cjpa.profile and pwa.policy_id=cjpa.policy
WHERE
pwa.user_work_id = 71072
and cjpa.profile IS NOT NULL

我想实现这个部分来续集

inner join company_job_profile_r_attendance cjpa on pwp.job_profile = cjpa.profile and pwa.policy_id=cjpa.policy

目前的情况是我可以做到这样

if(!module.exports.Placement.associations.JobProfileAttendance){
    module.exports.Placement.hasOne(module.exports.JobProfileAttendance, {
        as:'JobProfileAttendance',
        sourceKey:'jobProfile',
        foreignKey:'profile'
    })
}

但是这部分的结果是这样的

INNER JOIN "public"."company_job_profile_r_attendance" AS "Placement->JobProfileAttendance" ON "Placement"."job_profile" = "Placement->JobProfileAttendance"."profile" 

我想做这样的

INNER JOIN "public"."company_job_profile_r_attendance" AS "Placement->JobProfileAttendance" 
    ON "Placement"."job_profile" = "Placement->JobProfileAttendance"."profile" 
    AND "PeopleWorkAttendance"."policy_id" = "Placement->JobProfileAttendance"."policy"

想要添加一些 AND 条件但在 Sequelize 中

AND "PeopleWorkAttendance"."policy_id" = "Placement->JobProfileAttendance"."policy"

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    您可以在include 中使用on 选项,但请记住,您始终应该手动操作,因为 Sequelize 不支持复合主键和外键。
    请参阅findAll 选项中的options.include[].on

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-08
      • 2015-04-02
      • 1970-01-01
      • 2012-11-04
      • 2021-02-19
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      相关资源
      最近更新 更多