我想知道如何在额外字段上执行 where 条件。
目前我有3张桌子:
Courses : id, title, description,
Sections: id, title, description,
CoursesSections : id, fk_course_id, fk_section_id, position
目前我有这个要求,它可以让课程与部分按职位关联顺序:
let course = await db.Course.findOne({
where: {
id: parent.dataValues.id,
},
include: [
{
model: db.Section,
as: "Section",
through: {
attributes: ["position"],
},
},
],
order: [[Sequelize.literal("position"), "ASC"]],
})
但我想应用 where 条件,例如只有位置 1 或 2。
我试过这个,但它不起作用:
course = await db.Course.findOne({
where: {
id: 1,
},
include: [
{
model: db.Section,
as: "Section",
through: {
attributes: ["position"],
},
where: {
position: 2,
},
},
],
order: [[Sequelize.literal("position"), "ASC"]],
})
我有这个错误:
"消息": "on 子句"中的未知列'Section.position' ">
我把 where 条件放进去:
const course = await db.Course.findOne({
where: {
id: parent.dataValues.id,
},
include: [
{
model: db.Section,
as: "Section",
through: {
attributes: ["position"],
where: {
position: 1,
},
},
},
],
order: [[Sequelize.literal("position"), "ASC"]],
})