如何在节点中使用Sequelize自加入 [英] How to SELF JOIN using Sequelize in Node

查看:68
本文介绍了如何在节点中使用Sequelize自加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sequelize-cli进行迁移,并且已经研究了不同的场景,但是对于如何自我加入似乎没有确切的肯定答案.我想有一个称为友谊的联接表,该表将用户和朋友(本质上只是用户)联接起来.

I'm using sequelize-cli for migrations and have researched different scenarios but there doesn't seem to be a definite solid answer as to how to self join. I want to have a join table called friendships which joins users and friends (which are essentially just users).

更具体地说,是试图了解这种情况下通过"和作为"之间的区别.

More specifically trying to gain an understanding about the difference between "through" and "as" in this situation.

1)这是我的友谊表.被第二个协会绊倒了.我创建了两列,一列是userId,一列是friendId.我希望能够将此表链接到2个用户,每个表一个,别名为"friendId".

1) This is my friendship table. Getting tripped up on the second association. I have created two columns, one being userId, and one being friendId. I am hoping to be able to have this table linked to 2 users, one in each table, with one column aliased as "friendId".

```

'use strict';
module.exports = function(sequelize, DataTypes) {
  var friendship = sequelize.define('friendship', {
    userId: DataTypes.INTEGER,
    friendId: DataTypes.INTEGER
  }, {
    classMethods: {
      associate: function(models) {
        friendship.belongsTo(models.user);
        friendship.belongsTo(models.user { as: "friend" });
      }
    }
  });
  return friendship;
};

```

2)这是我的用户模型.再次强调,具体在关联对象中,我不清楚.这里的目标是能够呼叫user.friends并显示该用户的朋友的所有实例.不确定我是否正确使用了通过"和作为"关键字.

2) This is my user model. Again, specifically in the associations object is where I am not clear. The goal here is to be able to call user.friends and have all instances of that user's friends show up. Not certain I am using the "through" and "as" keywords right.

```

'use strict';
module.exports = function(sequelize, DataTypes) {
  var user = sequelize.define('user', {
    username: DataTypes.STRING,
    password: DataTypes.STRING,
    email: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        user.hasMany(models.friendship);
        user.hasMany(models.user { as: "friends", through: models.friendship });
      }
    }
  });
  return user;
};

```

推荐答案

存在多对多关系,它应该通过belongsToMany关联进行,不需要更多关系.甚至不需要定义好友表,sequelize会自动创建表

There is many-to-many relation, it should go through belongsToMany associations, no more relation is needed. There is no need even to define friends table, sequelize will create table automatically

'use strict';
module.exports = function(sequelize, DataTypes) {
      var user = sequelize.define('user', {
        username: DataTypes.STRING,
        password: DataTypes.STRING,
        email: DataTypes.STRING
      }, {
        classMethods: {
          associate: function(models) {
            user.belongsToMany(models.user, {
               as: "friends", 
               through: "friendship", 
               foreignKey: "userId", 
               otherKey: "friendId"
            });
          }
        }
      });
      return user;
};

这篇关于如何在节点中使用Sequelize自加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆