2017-04-12 31 views
0

風帆多對多關聯在添加關聯對象後會在保存時拋出錯誤。這是我的建模。將記錄添加到關聯的對象會拋出錯誤多對多水線風帆

User: 

module.exports = { 
    attributes: { 
    ... 
    operations: { 
     collection: 'operation', 
     via: 'users' 
    } 
    } 
}; 

操作:我的用戶的控制器上

module.exports = { 
    attributes: { 
    ... 
    users: { 
     collection: 'users', 
     via: 'operations', 
    } 
    } 
}; 

,我有:

addPermissionToUsers: function(req, res) { 
     Users.findOne(2).populate('operations').exec(function (err, user) { 

      if (err) throw err// handle error 
       // Queue up a record to be inserted into the join table 
       user.operations.add(1); 
      Save the user, creating the new associations in the join table 
      user.save(function (err) {  
       if (err) throw err 
        res.json(user) 
      }); 
     }); 
    } 

以下是錯誤跟蹤:

  if (err) throw err 
         ^
Error (E_UNKNOWN) :: Encountered an unexpected error 
    at new WLError (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/error/WLError.js:25:15) 
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/model/lib/defaultMethods/save.js:188:17 
    at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:52:16 
    at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:550:17 
    at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:544:17 
    at _arrayEach (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:85 
+0

你也有'用戶'模型嗎? – Sangharsh

+0

是的,我確實有一個用戶。 –

+0

不是'用戶',你有'用戶'模型嗎?這一行('collection:'users',')引用'Users'模型而不是'User'。 – Sangharsh

回答

0

噢,我的上帝!對於面臨此問題的任何人,您只需運行一次遷移,以便風帆可以自動爲您創建聯合表格。對我來說,我更改了config/model.js migrate: 'drop',並且帆添加了聯合表,它的作用就像魅力。

相關問題