我正在寻找一种使用SequelizeJS编写查询的方法,该方法包含一个引用多个模型(表)的OR子句。
我希望sql最终看起来像这样:
select *
from Department
inner join Office on Department.OfficeId = Office.Id
where Office.Name like 'spokane%'
or Department.Name like 'spokane%'
我已经看到有关过滤联接表的类似问题,答案如下所示。
options.include = [{
model: offices.model,
where: {
name: {
$like: 'spokane%'
}
}
}];
options.where = Sequelize.or(
{
'name': {
$like: 'spokane%'
}
});
departments.findAndCount(options);
然而,这并没有产生正确的sql。
它吐出这样的东西:
select *
from Department
inner join Office on Department.OfficeId = Office.Id and Office.name like 'spokane%'
where Department.name like 'spokane%'
非常感谢任何帮助。
答案 0 :(得分:3)
options.where = {
$or: [
sequelize.where(sequelize.col('office.name'), { $like: 'foo'}),
sequelize.where(sequelize.col('department.name'), { $like: 'foo'})
]
}