gpt4 book ai didi

sequelize.js - Sequelize OR 子句与多个模型

转载 作者:行者123 更新时间:2023-12-03 22:16:45 25 4
gpt4 key购买 nike

我正在寻找一种使用 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%'

如有任何帮助,我们将不胜感激。

最佳答案

options.where = {
$or: [
sequelize.where(sequelize.col('office.name'), { $like: 'foo'}),
sequelize.where(sequelize.col('department.name'), { $like: 'foo'})
]
}

关于sequelize.js - Sequelize OR 子句与多个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33271413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com