2016-12-07 72 views
0

方言:Postgres的
數據庫版本:@latest
Sequelize版本:@latest發現使用模型關聯

我試圖找出如何使用聯營模式。我有3種型號:postpostCityregion。它們具有以下關係: ​​聯想到post (post_id)region (region_id)。我使用的是搜索功能是這樣的:

include: [ 
    { 
     model: models.postCity, 
     include:[{model:models.region}], 
     attributes: [[models.sequelize.fn('count', 'post_id'), 'count']], 
    } 
    ], 
    where: { 
    $or: [ 
     { 
      "create_by" : {$not: 67} 
     }, 
     { 
     // "postCities.region_name":{$iLike: "%Guangazhou2%"} 
     }, 
     { 
      "description":{$iLike: "%India%"} 
     } 
     ] 
    } 

導致:

SELECT "post"."post_id", "post"."description", "post"."create_by", 
     "post"."create_time", "post"."update_time", "post"."country_id", 
     "postCities"."post_id" AS "postCities.post_id", 
     "postCities"."region_id" AS "postCities.region_id", 
     "postCities"."order_no" AS "postCities.order_no", 
     "postCities.region"."region_id" AS "postCities.region.region_id", 
     "postCities.region"."region_name" AS "postCities.region.region_name", 
     "postCities.region"."country_id" AS "postCities.region.country_id", 
     "postCities.region"."province_id" AS "postCities.region.province_id" 
FROM "t_post" AS "post" 
    LEFT OUTER JOIN "t_post_city" AS "postCities" 
     ON "post"."post_id" = "postCities"."post_id" 
    LEFT OUTER JOIN "t_region" AS "postCities.region" 
     ON "postCities"."region_id" = "postCities.region"."region_id" 
WHERE ("post"."create_by" != 67 OR "post"."description" ILIKE '%India%'); 

當我取消"postCities.region_name":{$iLike: "%Guangazhou2%"}然後我得到這個錯誤

column post.postCities.region_name does not exist 

我只是想我的查詢像這樣

... WHERE ("post"."create_by" != 67 
      OR "post"."description" ILIKE '%India%' 
      OR "postCities.region_name" ILIKE: "%Guangazhou2%") 

更新

我也試過包括[{model:models.region, where:{"region_name":{$iLike: "%Guangazhou2%"}}}]但這並沒有給我適當的結果。

回答

1

爲了增加條件包括表,應換用條件符號$,喜歡的話:

include: [{ 
    model: models.postCity, 
    include:[{model:models.region}], 
    attributes: [[models.sequelize.fn('count', 'post_id'), 'count']], 
}], 
where: { 
    $or: [{ 
     "create_by" : {$not: 67} 
    }, { 
     "$postCities.region.region_name$":{$iLike: "%Guangazhou2%"} 
    }, { 
     "description":{$iLike: "%India%"} 
    }] 
}