【问题标题】:Sails.js Same Model many to many associationSails.js Same Model 多对多关联
【发布时间】:2014-12-01 09:52:50
【问题描述】:

Sails.js .10 rc8

我已经完全没有这个想法了

我有一个名为 User 的模型,我想将它在一个集合中与其他用户相关联,例如朋友列表。就像一个多对多的关联

   //User.js
   friends: {
     collection: 'user',
     via: 'friends'
   }

但是当我跑步时 .populate('friends') 它不填充任何内容。有什么想法吗?

【问题讨论】:

  • 您找到解决方案了吗?我目前正在尝试做完全相同的事情..
  • 嗨,Michael,这是问题所在,因为它是同一个模型,这意味着它们都有多对多关联。 Waterline auto 创建了一个连接表,但它破坏了主导地位。所以你不能在它上面使用填充功能。我发现这样做的最好方法是创建另一个模型(例如 Friends.js)并拥有一个用户模型,然后是其他用户模型的集合。然后我设置了一个钩子,当我需要它时“填充”它。
  • 谢谢斯科特!这正是我要去的地方,但很高兴听到它实际上对你有用。我会继续沿着这条路前进。
  • @Michael 大家好,只是跟进一下:如果省略 via,这个 SO 问题中的用法应该 可以正常工作;但就像 Scott 指出的那样,在几个不同的补丁版本中存在一个挥之不去的问题。也就是说,据我所知,这个问题现在在 Sails v0.12 中得到了解决。因此,如果您仍然看到不一致的行为,我想知道,我很乐意提供帮助!请拨打on issue #1405,其中列出了cases we've tested thus far

标签: node.js model sails.js waterline


【解决方案1】:

您应该使用.populate('friends') 而不是.populate(friends)

【讨论】:

  • 非常感谢您的回复。当我的意思是 .populate('friends') 但它不起作用时:-(
  • @scott 试一试:collection: 'User',
【解决方案2】:

您的模型应该如下所示...

//User.js
friends: {
  collection: 'friend',
  via: 'user'
}

//Friend.js
user: {
  model: 'user'
  via: 'friends'
}

还帆 10 rc8 是旧的。你应该在帆 10.5

【讨论】:

    【解决方案3】:

    事实证明,当您在水线中有一个多对多关联时,您必须将其中一个模型声明为占主导地位。由于它们是相同的模型,因此两者都不占主导地位。创建连接表时,无法填充它。

    【讨论】:

    • Dominant 用于跨适配器查询,而不是您所描述的。 sailsjs.com/documentation/concepts/models-and-orm/associations/… 它只是说要在哪里创建助手表
    • 当您在多对多关联上设置主宰时,您是正确的,它确实说明了在发生跨适配器查询时连接表的位置。但是,它也会影响连接表的命名顺序。正常情况user_friends__friend_users 这些名称很重要,因为它们将是模型和属性的名称。但是,这些不能在同一个模型中(参见 OP cmets)。由于水线错误,连接表出现错误。如果我记得,连接表的名称是 user_friends__users_friends 或类似的表 users 不存在。
    【解决方案4】:

    我发现实现这一点的最佳方法实际上是添加对id 的引用。

    检查此用户模型:

    module.exports = {
      attributes: {
        name: {
          type: 'string',
          required: true,
          minLength: 2
        },
        email: {
          type: 'email',
          required: true,
          unique: true
        },
        friends: {
          collection: 'user',
          via: 'id'
        }
      }
    };
    

    现在,如果您运行sails console,您可以测试以下命令:

    User.create({name:'test',email:'test@test.com'}).exec(console.log);
    

    返回:

    { name: 'test',
      email: 'test@test.com',
      createdAt: '2016-12-01T22:06:19.723Z',
      updatedAt: '2016-12-01T22:06:19.723Z',
      id: 1 }
    

    您创建了第一个用户。让我们创建一些其他的:

    User.create({name:'test2',email:'test2@test.com'}).exec(console.log);
    

    导致:

     { name: 'test2',
      email: 'test2@test.com',
      createdAt: '2016-12-01T22:06:40.808Z',
      updatedAt: '2016-12-01T22:06:40.808Z',
      id: 2 }
    

    让我们看看到目前为止我们有什么:

    User.find().populate('friends').exec(console.log);
    
    [ { friends: [],
        name: 'test',
        email: 'test@test.com',
        createdAt: '2016-12-01T22:06:19.723Z',
        updatedAt: '2016-12-01T22:06:19.723Z',
        id: 1 },
      { friends: [],
        name: 'test2',
        email: 'test2@test.com',
        createdAt: '2016-12-01T22:06:40.808Z',
        updatedAt: '2016-12-01T22:06:40.808Z',
        id: 2 } ]
    

    现在,让我们使用对第一个用户的引用创建一个有朋友的用户。请注意,我只传递了一个 id,而不是数组:

    User.create({name:'test3',email:'test3@test.com', friends:1}).exec(console.log);
    

    现在,结果就是这个。请注意,“朋友”没有出现。这是设计使然。

    { name: 'test3',
      email: 'test3@test.com',
      createdAt: '2016-12-01T22:07:34.988Z',
      updatedAt: '2016-12-01T22:07:34.994Z',
      id: 3 }
    

    让我们使用populate 进行查找以查看当前状态:

    User.find().populate('friends').exec(console.log);
    

    现在我们看到第三个用户有朋友了。其他的有一个空数组。

     [ { friends: [],
        name: 'test',
        email: 'test@test.com',
        createdAt: '2016-12-01T22:06:19.723Z',
        updatedAt: '2016-12-01T22:06:19.723Z',
        id: 1 },
      { friends: [],
        name: 'test2',
        email: 'test2@test.com',
        createdAt: '2016-12-01T22:06:40.808Z',
        updatedAt: '2016-12-01T22:06:40.808Z',
        id: 2 },
      { friends: 
         [ { name: 'test',
             email: 'test@test.com',
             createdAt: '2016-12-01T22:06:19.723Z',
             updatedAt: '2016-12-01T22:06:19.723Z',
             id: 1 } ],
        name: 'test3',
        email: 'test3@test.com',
        createdAt: '2016-12-01T22:07:34.988Z',
        updatedAt: '2016-12-01T22:07:34.994Z',
        id: 3 } ]
    

    让我们创建第四个,这次和两个朋友一起:

    User.create({name:'test4',email:'test4@test.com', friends:[1,2]}).exec(console.log);
    

    导致(同样,没有朋友属性):

     { name: 'test4',
      email: 'test4@test.com',
      createdAt: '2016-12-01T22:07:50.539Z',
      updatedAt: '2016-12-01T22:07:50.542Z',
      id: 4 }
    

    这一次,我们将一个 id 数组传递给 friends。让我们看看目前的状态:

    User.find().populate('friends').exec(console.log);
    
     [ { friends: [],
        name: 'test',
        email: 'test@test.com',
        createdAt: '2016-12-01T22:06:19.723Z',
        updatedAt: '2016-12-01T22:06:19.723Z',
        id: 1 },
      { friends: [],
        name: 'test2',
        email: 'test2@test.com',
        createdAt: '2016-12-01T22:06:40.808Z',
        updatedAt: '2016-12-01T22:06:40.808Z',
        id: 2 },
      { friends: 
         [ { name: 'test',
             email: 'test@test.com',
             createdAt: '2016-12-01T22:06:19.723Z',
             updatedAt: '2016-12-01T22:06:19.723Z',
             id: 1 } ],
        name: 'test3',
        email: 'test3@test.com',
        createdAt: '2016-12-01T22:07:34.988Z',
        updatedAt: '2016-12-01T22:07:34.994Z',
        id: 3 },
      { friends: 
         [ { name: 'test',
             email: 'test@test.com',
             createdAt: '2016-12-01T22:06:19.723Z',
             updatedAt: '2016-12-01T22:06:19.723Z',
             id: 1 },
           { name: 'test2',
             email: 'test2@test.com',
             createdAt: '2016-12-01T22:06:40.808Z',
             updatedAt: '2016-12-01T22:06:40.808Z',
             id: 2 } ],
        name: 'test4',
        email: 'test4@test.com',
        createdAt: '2016-12-01T22:07:50.539Z',
        updatedAt: '2016-12-01T22:07:50.542Z',
        id: 4 } ]
    

    现在,如何向现有用户添加朋友?这有点奇怪:您必须首先find 用户,然后在生成的模型上使用add 函数,然后save 它。在我的日常代码中,我有一个辅助函数可以做到这一点。所以,这是一个例子:

    User.findOne(1).populate('friends').exec(function(err,u){ u.friends.add(3);u.save(function(err){ if(err) console.error(err);});});
    

    现在在控制台中我们看不到任何结果。 现在让我们检查一下用户内容:

    User.find().populate('friends').exec(console.log);
    
    [ { friends: 
         [ { name: 'test3',
             email: 'test3@test.com',
             createdAt: '2016-12-01T22:07:34.988Z',
             updatedAt: '2016-12-01T22:07:34.994Z',
             id: 3 } ],
        name: 'test',
        email: 'test@test.com',
        createdAt: '2016-12-01T22:06:19.723Z',
        updatedAt: '2016-12-01T22:09:41.410Z',
        id: 1 },
      { friends: [],
        name: 'test2',
        email: 'test2@test.com',
        createdAt: '2016-12-01T22:06:40.808Z',
        updatedAt: '2016-12-01T22:06:40.808Z',
        id: 2 },
      { friends: 
         [ { name: 'test',
             email: 'test@test.com',
             createdAt: '2016-12-01T22:06:19.723Z',
             updatedAt: '2016-12-01T22:09:41.410Z',
             id: 1 } ],
        name: 'test3',
        email: 'test3@test.com',
        createdAt: '2016-12-01T22:07:34.988Z',
        updatedAt: '2016-12-01T22:07:34.994Z',
        id: 3 },
      { friends: 
         [ { name: 'test',
             email: 'test@test.com',
             createdAt: '2016-12-01T22:06:19.723Z',
             updatedAt: '2016-12-01T22:09:41.410Z',
             id: 1 },
           { name: 'test2',
             email: 'test2@test.com',
             createdAt: '2016-12-01T22:06:40.808Z',
             updatedAt: '2016-12-01T22:06:40.808Z',
             id: 2 } ],
        name: 'test4',
        email: 'test4@test.com',
        createdAt: '2016-12-01T22:07:50.539Z',
        updatedAt: '2016-12-01T22:07:50.542Z',
        id: 4 } ]
    

    使用此方法,您甚至可以将同一用户添加到好友收藏中!

    User.findOne(1).populate('friends').exec(function(err,u){ u.friends.add(1);u.save(function(err){ if(err) console.error(err);});});
    

    玩得开心!

    【讨论】:

      猜你喜欢
      • 2015-06-27
      • 2015-06-01
      • 1970-01-01
      • 2015-02-21
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多