【问题标题】:Performing associations on a joining table in Node.js Sequelize在 Node.js Sequelize 中对连接表执行关联
【发布时间】:2018-09-21 14:59:00
【问题描述】:

在 Postgres DB 上使用 Node.js Sequelize。我有以下三个表:Groups、Applications 和一个用于映射关系 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;
};

【问题讨论】:

    标签: node.js postgresql sequelize.js


    【解决方案1】:

    您不需要定义GroupApplication,它会被推断出来,foreignKey 也是如此。关系应该在Group 和Application 之间(单数更好,把它想象成一个对象模型,你有一个Instance 和一个Application Model),但要加入through group_application桌子。完成此操作后,您可以将 include 每个 Models 放入另一个查询中。

    请注意,我没有测试过这段代码。

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

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

    example.js

    // return all Groups and their Applications
    const groupsToApplications = await Group.findAll({
      include: [
        {
          model: Application,
          as: 'applications',
          through: 'group_application',
        },
      ],
    });
    
    // return all Applications and their Groups
    const applicationsToGroups = await Application.findAll({
      include: [
        {
          model: Group,
          as: 'groups',
          through: 'group_application',
        },
      ],
    });
    

    【讨论】:

      猜你喜欢
      • 2019-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      • 2020-04-23
      相关资源
      最近更新 更多