我有3张桌子:
他们的联系是, 职位发布具有许多招聘阶段, 招聘阶段有很多面试机会
通过包含招聘阶段关联和组条款,我可以获得工作岗位的所有招聘阶段。
const jobPosts = await JobPost.unscoped().findAll({
where,
include: [
{
model: db.RecruitmentPhase,
include: [{
model: db.InterviewSlot,
},
],
group: ['RecruitmentPhases.id'],
});
尽管招聘阶段有很多个面试机会,但招聘阶段我只有一个面试机会。
我尝试在include中执行group子句。
const jobPosts = await JobPost.unscoped().findAll({
where,
include: [
{
model: db.RecruitmentPhase,
group: ['InterviewSlots.id'],
include: [{
model: db.InterviewSlot,
},
],
group: ['RecruitmentPhases.id'],
});
但它也只提供了一个一个面试机会
编辑
职位模型:
module.exports = (sequelize, DataTypes) => {
const jobPost = sequelize.define('JobPost', {
id: {
type: DataTypes.BIGINT,
allowNull: true,
autoIncrement: true,
primaryKey: true,
},
jobTitle: {
type: DataTypes.STRING(150),
allowNull: true,
},
}, {
timestamps: true,
defaultScope: {
attributes: { exclude: ['createdAt', 'updatedAt'] },
},
});
jobPost.associate = (models) => {
jobPost.hasMany(models.RecruitmentPhase);
};
return jobPost;
};
招聘阶段模型:
module.exports = (sequelize, DataTypes) => {
const recruitmentPhase = sequelize.define('RecruitmentPhase', {
id: {
type: DataTypes.BIGINT,
allowNull: true,
autoIncrement: true,
primaryKey: true,
},
phaseName: {
type: DataTypes.STRING(200),
allowNull: true,
},
}, {
timestamps: true,
});
recruitmentPhase.associate = (models) => {
recruitmentPhase.belongsTo(models.JobPost);
recruitmentPhase.hasMany(models.InterviewSlot);
};
return recruitmentPhase;
};
采访插槽型号:
module.exports = (sequelize, DataTypes) => {
const interviewSlot = sequelize.define('InterviewSlot', {
id: {
type: DataTypes.BIGINT,
allowNull: true,
autoIncrement: true,
primaryKey: true,
},
interviewDate: {
type: DataTypes.DATE,
allowNull: true,
},
});
interviewSlot.associate = (models) => {
interviewSlot.belongsTo(models.RecruitmentPhase);
};
return interviewSlot;
};
答案 0 :(得分:0)
删除group: ['RecruitmentPhases.id'],
以查看InterviewSlot的详细信息。照原样,您会看到采访时段的摘要...
答案 1 :(得分:0)
所以,我不太确定,这是正确的答案,也是正确的答案。 但是,当我使用此'->'对嵌套表'interviewslot'进行分组时,它起作用了。
group: ['RecruitmentPhases.id', 'RecruitmentPhases->InterviewSlots.id'],
答案 2 :(得分:-1)
如果您要从数据库中获取内容,为什么您似乎要获取JobPost
而不是RecruitmentPhase
?
据我了解,您正在寻求获取所有RecruitmentPhases
及其工作职位,并附上为每个InterviewSlots
分配的RecruitmentPhase
。
可能的代码: (已更新)
const rec = await db.RecruitmentPhase.findAll({
include:[{model: db.JobPost, where:{ id:job_id }}, {model: db.InterviewSlot}]
});
res.json(rec)
//Expected JSON Data
{
"RecruitmentPhase":[
{
"id":1,
"phaseName":"Phase 1",
"JobPosts": {
"id":1,
"jobTitle":"XYZ"
},
"InterviewSlots":[
{//inteview slot #1 data},
{//interview slot #2 data}
]
},
{
"id":2,
"phaseName":"Phase 2",
"JobPosts": {
"id":1,
"jobTitle":"XYZ"
},
"InterviewSlots":[
{//inteview slot #1 data},
{//interview slot #2 data}
]
}
]
}