在 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;
};
最佳答案
您不需要定义 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',
},
],
});
关于node.js - 在 Node.js Sequelize 中对连接表执行关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52446408/