左外部联接具有自定义ON条件

问题描述

我正在使用续集,DB作为Postgres

我的学生以(id,name,dept_id)列的形式出现。

我的部门表以(id,dname,hod)列

我的协会看起来像这样

Student.hasOne(models.Department,{
    foreignKey: 'id',as : "role"
  });

我想找一个具有特定ID及其部门详细信息的学生。通过使用sequelize,我编写了如下查询

Student.findOne({
    where: { id: id },include: [{
        model: Department,as:"dept"
    }],})

FYI在findOne中包含此选项,它是按如下方式生成的左外部联接查询

SELECT "Student"."id","Student"."name","Student"."dept_id","dept"."id" AS "dept.id","dept"."dname" AS "dept.dname","dept"."hod" AS "dept.hod",FROM "students" AS "Student" 
LEFT OUTER JOIN "departments" AS "dept" 
ON "Student"."id" = "dept"."id" 
WHERE "Student"."id" = 1 
LIMIT 1;

我的预期输出应该是

{
    id: 1,name: "bod",dept_id: 4,dept: {
        id: 4,dname: "xyz",hod: "x"
    }
}

但是我得到了

{
    id: 1,dept: {
        id: 1,dname: "abc",hod: "a"
    }
}

所以我如何将接通条件更改为

ON "Student"."dept_id" = "dept"."id"

先谢谢您

解决方法

Student.hasOne(models.Department,{
    foreignKey: 'dept_id',// fixed
    as : "role"
  });

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...