如何在 Node 中使用 Sequelize 进行 SELF JOIN [英] How to SELF JOIN using Sequelize in Node

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

问题描述

我正在使用 sequelize-cli 进行迁移,并研究了不同的场景,但对于如何自我加入似乎没有明确的答案.我想要一个名为friendships的连接表,它连接用户和朋友(本质上只是用户).

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 并显示该用户朋友的所有实例.不确定我是否正确使用了through"和as"关键字.

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;
};

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

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