2014-10-07 80 views
0

這將返回適量的記錄,但帳戶僅填充到第一條記錄中。查詢水線生成也是正確的,並返回每條記錄的所有相關數據。 populateAll()沒有效果。交換.then對於.exec沒有任何影響。檢索多個記錄上的一對一關聯

此外,確實顯示爲關聯的一個「帳戶」甚至不是正確的記錄。

我做錯了什麼或這是一個錯誤?

pic

mail_items.find().populate('account').then(function(mail_items) { 
    return mail_items; 
}); 

accounts.js

module.exports = { 
    tableName : 'accounts', 

    attributes : { 
     account_id : { 
      type : 'int', 
      primaryKey : true, 
      unique : true 
     }, 

     name : 'string' 
    } 
}; 

mail_items.js

module.exports = { 
    tableName : 'mail_items', 

    attributes : { 
     mail_item_id : { 
      type : 'int', 
      primaryKey : true, 
      unique : true 
     }, 

     sender : 'string', 

     account : { 
      columnName : 'account_id', 
      type : 'int', 
      model : 'accounts' 
     } 

    } 
}; 

回答

0

這是通過自動生成賬戶模型的ID相關聯的表時,工作礦井。

account.js

module.exports = { 
     name : 'string' 
    } 
}; 

mail_items.js

module.exports = { 
    //tableName : 'mail_items', 
    //table name exactly same to model name 
    attributes : { 
     mail_item_id : { 
      type : 'int', 
      primaryKey : true, 
      unique : true 
     }, 

     sender : 'string', 

     account : { 
      //columnName : 'account_id', 
      //type : 'int', 
      model : 'accounts' 
     } 

    } 
}; 
+0

它的確工作。當然,每張桌子的pk不需要是id ...對嗎?如果沒有,我可以忍受它。 – brian 2014-10-07 09:19:44

+0

是的,但帆是自動生成帶主鍵和自動遞增屬性的'ID'。 – vusan 2014-10-07 09:23:56

+0

不填充模型的一個原因是,可能不存在使用mail_items鏈接的id的帳戶。 – vusan 2014-10-07 09:27:32

相關問題