我有2个表,它们之间是一对多的关系(课程中有很多科目),在对主题执行删除操作之前,我想检查它是否与任何课程相关联,所以这里是我的模型:>
课程:
'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())
并出现错误:
执行(默认):SELECT
Course
。id
,Course
。name
,Course
。desc
,Course
。createdAt
,Course
。updatedAt
,Course
。courseId
,courseSubjects
。createdAt
如courseSubjects.createdAt
,{ {1}}。courseSubjects
ASupdatedAt
,courseSubjects.updatedAt
。courseSubjects
ASCourseId
,courseSubjects.CourseId
。courseSubjects
AS {{1 }}从SubjectId
到courseSubjects.SubjectId
,内部连接Courses
到Course
到courseSubjects
。courseSubjects
=Course
。{{1} } ANDid
。courseSubjects
= 34; [0]未处理的拒绝SequelizeDatabaseError:“字段列表”中的未知列“ Course.courseId”
我不明白为什么要尝试选择“ courseId”。请帮助我解决这个问题。
答案 0 :(得分:0)
您还需要在课程表中添加关系,这将从选择中删除“ courseId”列。