【发布时间】:2020-06-25 17:07:04
【问题描述】:
我想知道如何从链接器表中获取额外的列。
目前我有 3 张桌子:
Courses : id, title, description,
Sections: id, title, description,
CoursesSections : id, fk_course_id, fk_section_id, position
我尝试使用这个来获取课程,但我不明白如何从链接器表中获取“位置”:
db.Course.findOne({
where: {
id: 1,
},
include: [
{
model: db.Section,
as: "Section",
through: {
attributes: ["position"],
},
},
],
order: [[Sequelize.literal("position"), "ASC"]],
}).then((response) => {
console.log(response.dataValues.position) // <= undefined
console.log(response.dataValues.Section.position) // <= <= undefined
return response.dataValues.Section // <= get without "position" field
})
课程模式:
Course.associate = (models) => {
models.Course.belongsToMany(models.Section, {
as: "Section",
through: "CourseSection",
foreignKey: "fk_course_id",
otherKey: "fk_section_id",
})
}
截面模型:
Section.associate = (models) => {
models.Section.belongsToMany(models.Course, {
as: "Course",
through: "CourseSection",
foreignKey: "fk_section_id",
otherKey: "fk_course_id",
})
}
【问题讨论】:
标签: node.js sequelize.js