【问题标题】:extra columns with Sequelize many to many带有 Sequelize many to many 的额外列
【发布时间】: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


    【解决方案1】:

    创建一个 CourseSection 模型:

    CourseSection.associate = (models) => {
        models.CourseSection.belongsToMany(models.Course, {
            through: "CoursesSections",
            foreignKey: "fk_course_id",
        })
        models.CourseSection.belongsToMany(models.Section, {
            through: "CoursesSections",
            foreignKey: "fk_section_id",
        })
    }
    

    将 CourseSection 与 Course 和 Section 相关联,如下所示:

    Course.associate = (models) => {
        models.Course.belongsToMany(models.Section, {
            as: "Section",
            through: models.CourseSection,
            foreignKey: "fk_course_id",
            otherKey: "fk_section_id",
        })
    }
    
    Section.associate = (models) => {
        models.Section.belongsToMany(models.Course, {
            as: "Course",
            through: models.CourseSection,
            foreignKey: "fk_section_id",
            otherKey: "fk_course_id",
        })
    }
    

    用这个获取位置字段:

    const course = await db.Course.findOne({
        where: {
            id: parent.dataValues.id,
        },
        include: [
            {
                model: db.Section,
                as: "Section",
                through: {
                    attributes: ["position"],
                },
            },
        ],
        order: [[Sequelize.literal("position"), "ASC"]],
    })
    
    course.Section.forEach((section) => {
        console.log(section.dataValues.coursessections.position)
    
        section.position = section.dataValues.coursessections.position
    })
    
    return course.dataValues.Section
    

    【讨论】:

    • 谢谢@Anatoly!我更新了你的帖子,做了一些修改!
    猜你喜欢
    • 2019-04-27
    • 2016-11-05
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-28
    • 1970-01-01
    • 2020-12-17
    相关资源
    最近更新 更多