在Node.js Sequelize中的联接表上执行关联

时间:2018-09-21 14:59:01

标签: node.js postgresql sequelize.js

在Postgres DB上使用Node.js Sequelize。我有以下三个表:应用程序和一个联接表,用于映射该关系 GroupApplication 。 GroupApplication使用ID作为外键。当我在sequelize中的GroupApplication表上查找所有内容时,我希望将各个组和应用程序的“名称”字段加入到各个表中。因此,我想加入

Groups                            Applications    

| id| name     | description |       | id| name   | description |
| --|:--------:| -----------:|       | --|:------:| -----------:|
| 1 | admin    | First Group |       | 1 | typer  | Types       |
| 2 | primary  | Second Group|       | 2 | tester | Tests       |
| 3 | secondary| Third Group |       | 3 | zestrer| Zests       |


GroupApplications
| id| groups_id | application_id |
| --|:---------:| --------------:|
| 1 | 1         | 1              | 
| 2 | 1         | 2              |
| 3 | 1         | 3              |
| 4 | 2         | 2              |
| 5 | 2         | 3              |
| 6 | 3         | 2              |

The resulting join:

GroupApplications
| id|groups_id|group_name|application_id|application_name|
| --|:-------:|:--------:|:------------:|---------------:|
| 1 | 1       | admin    |1             |typer           |    
| 2 | 1       | admin    |2             |tester          |
| 3 | 1       | admin    |3             |zester          |
| 4 | 2       | primary  |2             |tester          |
| 5 | 2       | primary  |3             |zester          |
| 6 | 3       | secondary|2             |tester          |

我有以下文件:

groups.js       

module.exports = function (sequelize, DataTypes) {
  var Groups = sequelize.define('Groups', {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true
    },
      name: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true
    }
  }, {
    timestamps: false,
    tableName: 'groups',
  });

  Groups.associate = function(models) {
    Groups.belongsToMany(models.GroupApplication, { through: 'group_application', foreignKey: 'group_id' });
  };

return Groups;
};


applications.js       

module.exports = function (sequelize, DataTypes) {
  var Applications = sequelize.define('Applications', {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true
    },
      name: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true
    }
  }, {
    timestamps: false,
    tableName: 'groups',
  });

  Applications.associate = function(models) {
    Applications.belongsToMany(models.GroupApplication, { through: 'group_application', foreignKey: 'application_id' });
  };

return Applications;
};

group-application.js

module.exports = function (sequelize, DataTypes) {
  let GroupApplication = sequelize.define('GroupApplication', {
    group_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true
    },
    application_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true
    }
  }, {
    timestamps: false,
    tableName: 'group_application',
  });

  return GroupApplication;
};

1 个答案:

答案 0 :(得分:1)

您无需定义GroupApplication,将与foreignKey一样进行推断。该关系应该在GroupApplication之间(单数更好,将其视为对象模型,其中您拥有Instance Application中的Model)但要使用through的{​​{1}}连接表。完成此操作后,您可以group_application的每个include进入另一个查询。

请注意,我尚未测试此代码。

group.js

Models

application.js

module.exports = function (sequelize, DataTypes) {
  var Group = sequelize.define('Group', 
    {
      name: {
        type: DataTypes.STRING,
        allowNull: false,
        unique: true
      },
    },
    {
      tableName: 'group',
      timestamps: false,
      underscored: true,
    }
  );

  Group.associate = function(models) {
    Group.belongsToMany(models.Application, {
      as: 'applications',           // <-- alias here
      through: 'group_application',
    });
  };

  return Group;
};

example.js

module.exports = function (sequelize, DataTypes) {
  var Application = sequelize.define('Application',
    {
      name: {
        type: DataTypes.STRING,
        allowNull: false,
        unique: true
      },
    },
    {
      tableName: 'application',
      timestamps: false,
      underscored: true,
    }
  );

  Application.associate = function(models) {
    Application.belongsToMany(models.Group, {
      as: 'groups',           // <-- alias here
      through: 'group_application',
    });
  };

  return Application;
};