我有一个项目模型,可以保存项目经理,设计师,用户体验和项目所有者等相关方所有者的ID。他们都是用户,我想获得包含所有者名称的allProjects。喜欢 id,pm,designer,ux,owner。 我的模特:
module.exports = function (sequelize, DataTypes) {
return sequelize.define('Project', {
Id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true,
field: 'id'
},
ownerId: {
type: DataTypes.INTEGER(11),
allowNull: true,
field: 'owner_id'
},
pmId: {
type: DataTypes.INTEGER(11),
allowNull: true,
field: 'pm_id'
},
designerId: {
type: DataTypes.INTEGER(11),
allowNull: true,
field: 'designer_id'
},
uxId: {
type: DataTypes.INTEGER(11),
allowNull: true,
field: 'ux_id'
}
}, {
tableName: 'project',
timestamps: false,
engine: 'InnoDB',
charset: 'utf8',
classMethods: {
associate(models) {
models.Project.belongsTo(models.User, {
'as': 'owner'
foreignKey: 'ownerId',
otherKey: 'id'
});
models.Project.belongsTo(models.User, {
'as': 'projectManager'
foreignKey: 'pmId',
otherKey: 'id'
});
models.Project.belongsTo(models.User, {
'as': 'designer'
foreignKey: 'designerId',
otherKey: 'id'
});
models.Project.belongsTo(models.User, {
'as': 'ux'
foreignKey: 'uxId',
otherKey: 'id'
});
})};
sequelize.define('User', {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: false
}
})