【问题标题】:Sequelize OR clause with multiple modelsSequelize OR 子句与多个模型
【发布时间】:2015-10-22 14:47:49
【问题描述】:

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

任何帮助将不胜感激。

【问题讨论】:

    标签: sequelize.js


    【解决方案1】:
    options.where = {
      $or: [
        sequelize.where(sequelize.col('office.name'), { $like: 'foo'}),
        sequelize.where(sequelize.col('department.name'), { $like: 'foo'})
      ]
    }
    

    【讨论】:

    • 谢谢!我不得不修改包含以使其成为必需,但后来效果很好。 { model: offices.model, required: true }
    • 感谢@JuanitoCROM,required 选项帮助了我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 2018-01-08
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    相关资源
    最近更新 更多