gpt4 book ai didi

node.js - sequelize 选择模型中未提及的列

转载 作者:行者123 更新时间:2023-12-03 22:21:59 25 4
gpt4 key购买 nike

我有 2 个表,它们是一对多的关系(类(class)有很多主题),在对主题进行删除操作之前,我想检查它是否与任何类(class)相关联,所以这是我的模型:

类(class):

    'use strict';
module.exports = (sequelize, DataTypes) => {
const Course = sequelize.define('Course', {
name: DataTypes.STRING,
desc: DataTypes.TEXT
}, {});
Course.associate = function(models) {
Course.belongsToMany(models.Subject, {
through: 'courseSubjects'
});

Course.hasMany(models.Batch)
};
return Course;
};

和主题:

    'use strict';
module.exports = (sequelize, DataTypes) => {
const Subject = sequelize.define('Subject', {
name: DataTypes.STRING,
desc: DataTypes.TEXT
}, {});

Subject.associate = function(models) {
Subject.belongsToMany(models.Module, {
through: 'subjectModules'
});

Subject.belongsToMany(models.Course, {
through: 'courseSubjects',
});
};
return Subject;
};

所以这是我调用的语句:

res.status(200).send(subject.getCourses())

出现错误:

Executing (default): SELECT Course.id, Course.name, Course.desc, Course.createdAt, Course.updatedAt, Course.courseId, courseSubjects.createdAt AS courseSubjects.createdAt, courseSubjects.updatedAt AS courseSubjects.updatedAt, courseSubjects.CourseId AS courseSubjects.CourseId, courseSubjects.SubjectId AS courseSubjects.SubjectId FROM Courses AS Course INNER JOIN courseSubjects AS courseSubjects ON Course.id = courseSubjects.CourseId AND courseSubjects.SubjectId = 34; [0] Unhandled rejection SequelizeDatabaseError: Unknown column 'Course.courseId' in 'field list'

我不明白为什么要尝试选择“courseId”。请帮我解决这个问题。

最佳答案

您还需要将关系添加到类(class)表中,它将从选择中删除“courseId”列。

关于node.js - sequelize 选择模型中未提及的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52372955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com