2017-06-22 117 views
0

我正在尋找使用sequelize.js來打一個電話和ping兩個表。使用sequelize.js創建與多個外鍵的關聯

我希望我的結果中找到我的第二個表中有兩個外鍵匹配相應的數據 - 「TIMEPERIOD」和「NODEID」

目前,無論不同的別名(S)的 - 只有我的查詢結果匹配我指定的最後一個外鍵。因此在這種情況下的時間段。

associate: function (models) { 
    models.belowOcfBudget.belongsTo(models.belowOcf, { 
     foreignKey: 'nodeid', 
     targetKey: 'nodeid', 
     as: 'belowOcf_id' 
    }); 
    models.belowOcfBudget.belongsTo(models.belowOcf, { 
     foreignKey: 'timeperiod', 
     targetKey: 'timeperiod', 
     as: 'belowOcf_time' 
    }); 
} 

下面是我在我的路由器中包含此關聯的地方。

BelowOCFBudget.findAll({ 
    raw: true, 
    include:[{ 
     model: models.belowOcf, 
     as: 'belowOcf_id' 
    }], 
    include:[{ 
     model: models.belowOcf, 
     as: 'belowOcf_time' 
    }] 

我怎麼能有我的查詢執行,並從具有匹配TIMEPERIOD和id兩個表返回我的數據集?

回答

0

你不應該包含你的關聯模型兩次。這導致在同一個表上的左加入,每個外鍵一個。 而是隻包含一次,並將第二個外鍵的匹配作爲附加where子句添加到include中。

沒有測試它,但應該是這個樣子:

BelowOCFBudget.findAll({ 
    raw: true, 
    include:[{ 
     model: models.belowOcf, 
     where: {belowOcf.timeperiod: timeperiod} 
    }], 
})