【发布时间】:2017-09-30 16:16:19
【问题描述】:
我正在尝试使用 Sequelize.js 在特定列上加入一些表。
到目前为止,我的代码是这样的:
table_1.findall({
include: [{
model: table_2
attributes: ['id', 'another_id']
include: [{
model: table_3
required: true
attributes: ['time']
}]
}]
})
每个表的主键是“id”。
这似乎等同于以下 SQL(为简洁起见,我显示 SELECT *,因为这不是本问题的重点):
SELECT *
FROM table_1 as t1
LEFT OUTER JOIN table_2 as t2 ON t1.id = t2.t1_id
INNER JOIN table_3 as t3 ON t2.id = t3.t2_id
我想要类似的东西:
SELECT *
FROM table_1 as t1
LEFT OUTER JOIN table_2 as t2 ON t1.id = t2.t1_id
INNER JOIN table_3 as t3 ON t2.another_id = t3.t2_id
有没有办法强制 t2 和 t3 之间的连接使用 t2 的主键以外的东西?
我找到了 [options.include[].on] 在Sequelize documentation 中,但不知道补充我自己的 ON 条件的语法是什么。
【问题讨论】:
标签: mysql sql node.js orm sequelize.js