顺序选择模型中未提及的列

时间:2018-09-17 17:27:24

标签: node.js sequelize.js

我有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 CourseidCoursenameCoursedescCoursecreatedAtCourseupdatedAtCoursecourseIdcourseSubjectscreatedAtcourseSubjects.createdAt,{ {1}}。courseSubjects AS updatedAtcourseSubjects.updatedAtcourseSubjects AS CourseIdcourseSubjects.CourseIdcourseSubjects AS {{1 }}从SubjectIdcourseSubjects.SubjectId,内部连接CoursesCoursecourseSubjectscourseSubjects = Course。{{1} } AND idcourseSubjects = 34;   [0]未处理的拒绝SequelizeDatabaseError:“字段列表”中的未知列“ Course.courseId”

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

1 个答案:

答案 0 :(得分:0)

您还需要在课程表中添加关系,这将从选择中删除“ courseId”列。