2015-10-22 48 views
1

我正在尋找一種使用SequelizeJS來編寫查詢的方法,該方法包含引用多個模型(表)的OR子句。多個模型的子句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%' 

任何幫助將不勝感激。

回答

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

謝謝!我不得不修改包含以使其成爲必需,但它後來效果很好。 '{model:offices.model,required:true}' – JuanitoCROM

+0

Thanks @JuanitoCROM,'required'選項幫助我 – YassinMK