【问题标题】:One to many association on Sails 0.10.x fails to retrieve owner data using populateSails 0.10.x 上的一对多关联无法使用填充检索所有者数据
【发布时间】:2014-09-03 21:41:21
【问题描述】:

我正在尝试使用带有 mongoDB 的sails 0.10.x 创建一个简单的一对多关联。 我有两个模型,游戏和地图,一个游戏可以有多个与其帐户关联的地图。 使用填充我可以正确获取与游戏关联的所有地图,但逆操作不起作用。我无法使用 Map 的填充获取游戏数据。

这是我的模型:

//Game.js
module.exports = {  
   attributes: {
    name:{
       type: 'string',
       required: true
     },

   maps: {
      collection: 'map',
      via: 'game'
   }
}

//Map.js
module.exports = {
    attributes: {    
        name: {
           type: 'string',
           required: true
     },

game:{
     model: 'game'
  }
}

//test data:
  Game.create({
    id: 200,
    name: 'game1'});

  Map.create({
    id: 300,
    name: 'map1',
    game: 200
  });

如果我使用

Game.find().populate("maps").exec(console.log);

它会正确返回与游戏关联的地图:

 { maps: 
 [ { name: 'map1',
     game: '200',
     id: '300' } ],
  name: 'game1'}

但是反向命令,试图从地图中检索游戏数据,返回一个未定义的游戏属性:

Map.find().populate("game").exec(console.log)

返回:

{ name: 'map1',
  game: undefined,
  id: '300' }

即使尝试在 find() 中使用参数,我也得到了相同的结果。

使用 Sails 和 waterline 文档仔细检查了我的代码,但无法弄清楚我做错了什么。

Sails 文档:http://sailsjs.org/#/documentation/concepts/ORM/Associations/OnetoMany.html

水线文档:https://github.com/balderdashy/waterline-docs/blob/master/associations.md

更新

我发现,如果我创建测试数据而不强制使用我想要的 ID,它会完美运行:

  Game.create({name: 'game1'}).exec(console.log);

  Game.findOne({name: 'game1'}, function (err, game){
    Map.create({
      name: 'map1',
      game: game.id
    }).exec(console.log);
  });

现在我可以获取所有者数据了。由于它在我使用 mongoDB id 时有效,因此我认为这是与连接器相关的一些问题。

Map.find().populate("game").exec(console.log)
//returns (ommited the original IDs
   [ { game:
     { name: 'game1',  id: 'xxx100' }, 
     name: 'map1',
     id: 'xxx300' } ] 

谢谢!

【问题讨论】:

    标签: sails.js waterline sails-mongo


    【解决方案1】:

    如果您使用的是 MongoDB,并且不想使用默认的 Mongo ID,则需要 configure your models accordingly。例如:

    //Game.js
    module.exports = {  
       // Don't use automatic primary key
       autoPK: false,
       attributes: {
        // Create your own ID field manually
         id: {
           type: 'integer',
           primaryKey: true
         },
         name:{
           type: 'string',
           required: true
         },
    
         maps: {
          collection: 'map',
          via: 'game'
         }
    
       }
    }
    

    这将允许关联和其他操作正常工作。请注意,如果您这样做,那么您将负责保证唯一的整数 ID; MongoDB 没有“自动增量”属性。

    【讨论】:

    • 感谢您的回复!我不知道 autoPK 属性。将其设置为 false 并手动创建字段 ID 后,我可以正确设置所需的 ID,并将其与填充一起使用。
    【解决方案2】:

    我发现,如果我创建测试数据而不强制使用我想要的 ID,它会完美运行:

    Game.create({name: 'game1'}).exec(console.log);
    
    Game.findOne({name: 'game1'}, function (err, game){
     Map.create({
       name: 'map1',
       game: game.id
     }).exec(console.log);
    });
    

    现在我可以获取所有者数据了。

    由于它在我使用 mongoDB id 时有效,我相信这是与连接器相关的一些问题。

    Map.find().populate("game").exec(console.log)
    //returns (ommited the original IDs)
     [ { game:
        { name: 'game1',  id: 'xxx100' }, 
          name: 'map1',
          id: 'xxx300' 
         } 
     ] 
    

    【讨论】:

      猜你喜欢
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-09
      • 2016-08-30
      • 2015-10-12
      • 2015-04-03
      相关资源
      最近更新 更多