【问题标题】:Adding a record to an associated object throws and error Many-to-many Waterline sails向关联对象添加记录会引发和错误多对多水线帆
【发布时间】:2017-04-12 06:07:26
【问题描述】:

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

【问题讨论】:

  • 你也有Users 模型吗?
  • 是的,我有一个用户。
  • 不是User,你有Users 型号吗?此行 (collection: 'users',) 指的是 Users 模型,而不是 User。

标签: sails.js waterline sails-postgresql


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-08-30
    • 1970-01-01
    • 2014-11-27
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    相关资源
    最近更新 更多