0

Sail 多对多关联在添加关联对象后在保存时抛出错误。这是我的建模。

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
4

1 回答 1

0

我的天啊!!对于遇到此问题的任何人,您只需要运行迁移,以便sails 可以自动为您创建联合表。对我来说,我更改了 config/model.jsmigrate: 'drop'并添加了联合表,它就像魅力一样工作。

于 2017-04-12T07:02:42.560 回答