关于@TophatGordon 在接受的答案评论中的疑问:如果我们需要在模型中设置任何关联。
还通过了仍处于 open 状态的github issue raised back in 2012。
所以我也遇到了同样的情况,并试图为左外连接设置我自己的ON 条件。
当我直接尝试在Table1.findAll(...include Table2 with ON condition...) 中使用on: {...} 时,它不起作用。
它抛出了一个错误:
EagerLoadingError [SequelizeEagerLoadingError]: Table2 is not associated to Table1!
我的用例是将 Table1 中的两个非主键列与左外连接中 Table2 中的两个列进行匹配。我将展示我是如何以及取得了什么成就的:
不要被表名和列名弄糊涂了,因为我必须从我使用的原始名称中更改它们。
所以我不得不在 Table1(Task) 中创建一个关联,例如:
Task.associate = (models) => {
Task.hasOne(models.SubTask, {
foreignKey: 'someId', // <--- one of the column of table2 - SubTask: not a primary key here in my case; can be primary key also
sourceKey: 'someId', // <--- one of the column of table1 - Task: not a primary key here in my case; can be a primary key also
scope: {
[Op.and]: sequelize.where(sequelize.col("Task.some_id_2"),
// '=',
Op.eq, // or you can use '=',
sequelize.col("subTask.some_id_2")),
},
as: 'subTask',
// no constraints should be applied if sequelize will be creating tables and unique keys are not defined,
//as it throws error of unique constraint
constraints: false,
});
};
所以查找查询看起来像这样:
Task.findAll({
where: whereCondition,
// attributes: ['id','name','someId','someId2'],
include: [{
model: SubTask, as: 'subTask', // <-- model name and alias name as defined in association
attributes: [], // if no attributes needed from SubTask - empty array
},
],
});
结果查询:
- 一个匹配条件取自 [foreignKey] = [sourceKey]
- 第二个匹配条件由
sequelize.where(...)获得,用于scope:{...}
select
"Task"."id",
"Task"."name",
"Task"."some_id" as "someId",
"Task"."some_id_2" as "someId2"
from
"task" as "Task"
left outer join "sub_task" as "subTask" on
"Task"."some_id" = "subTask"."some_id"
and "Task"."some_id_2" = "subTask"."some_id_2";
另一种实现与上述相同的方法来解决在包含中使用 Table1 时出现的问题,即当 Table1 显示为 2 级表或包含在其他表中时 - 比如说 Table0
Task.associate = (models) => {
Task.hasOne(models.SubTask, {
foreignKey: 'someId', // <--- one of the column of table2 - SubTask: not a primary key here in my case; can be primary key also
sourceKey: 'someId', // <--- one of the column of table1 - Task: not a primary key here in my case; can be a primary key also
as: 'subTask',
// <-- removed scope -->
// no constraints should be applied if sequelize will be creating tables and unique keys are not defined,
//as it throws error of unique constraint
constraints: false,
});
};
所以来自 Table0 的查找查询看起来像这样:也不考虑foreignKey 和sourceKey,因为我们现在将使用自定义on: {...}
Table0.findAll({
where: whereCondition,
// attributes: ['id','name','someId','someId2'],
include: {
model: Task, as: 'Table1AliasName', // if association has been defined as alias name
include: [{
model: SubTask, as: 'subTask', // <-- model name and alias name as defined in association
attributes: [], // if no attributes needed from SubTask - empty array
on: {
[Op.and]: [
sequelize.where(
sequelize.col('Table1AliasName_OR_ModelName.some_id'),
Op.eq, // '=',
sequelize.col('Table1AliasName_OR_ModelName->subTask.some_id')
),
sequelize.where(
sequelize.col('Table1AliasName_OR_ModelName.some_id_2'),
Op.eq, // '=',
sequelize.col('Table1AliasName_OR_ModelName->subTask.some_id_2')
),
],
},
}],
}
});
如果您的表已经创建,请跳过以下部分...
将约束设置为 false,就像 sequelize 尝试创建第二个表(子任务)一样,由于以下查询,它可能会抛出错误 (DatabaseError [SequelizeDatabaseError]: there is no unique constraint matching given keys for referenced table "task"):
如果不存在则创建表 "sub_task" ("some_id" INTEGER, "some_id_2"
INTEGER 在更新时删除级联时引用“任务”(“some_id”)
级联,“数据”整数);
如果我们设置 constraint: false,它会在下面创建这个查询,而不是在我们引用非主列时抛出唯一约束错误:
如果不存在则创建表 "sub_task" ("some_id" INTEGER, "some_id_2" INTEGER, "data" INTEGER);