Sequelize:“模型 A 与模型 B 没有关联”

Posted

技术标签:

【中文标题】Sequelize:“模型 A 与模型 B 没有关联”【英文标题】:Sequelize: "Model A is not associated to Model B" 【发布时间】:2021-10-19 10:10:00 【问题描述】:

我有问题:“未关联到”。我想要一对多关联。这是我的代码 Index.js

  fs.readdirSync(__dirname)
  .filter(file => 
    return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
  )
  .forEach(file => 
    const model = require(path.join(__dirname, file))(sequelize, Sequelize);
    db[model.name] = model;
  );

Object.keys(db).forEach(modelName => 
  if (db[modelName].associate) 
    db[modelName].associate(db);
  
);
db.Sequelize = Sequelize;
db.sequelize = sequelize;
module.exports = db;

这是 course.js

module.exports = (sequelize, Sequelize) => 
  const Course = sequelize.define(
    'course',
    
      idCourse: 
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        field: 'idcourse',
      ,
      nameOfCourse: 
        type: Sequelize.STRING,
        field: 'nameofcourse',
      ,
      idLevel: 
        type: Sequelize.INTEGER,
        field: 'idlevel',
      ,
      idTypeOfCourse: 
        type: Sequelize.INTEGER,
        field: 'idtypeofcourse',
      ,
      startDate: 
        type: Sequelize.DATE,
        field: 'startdate',
      ,
      endDate: 
        type: Sequelize.DATE,
        field: 'enddate',
      ,
      fee: 
        type: Sequelize.BIGINT,
        field: 'fee',
      ,
      isDeleted: 
        type: Sequelize.BOOLEAN,
        field: 'isdeleted',
      ,
    ,
    
      freezeTableName: true,

      timestamps: false,

      createdAt: false,

      updatedAt: false,
    
  );
  Course.associate = function (models) 
    models.course.belongsToMany(models.bill, 
      through: models.billinfo,
      as: 'bill',
      foreignKey: 'idCourse',
    );
    models.course.belongsTo(models.typeofcourse, 
      foreignKey: 'idTypeOfCourse',
      sourceKey: 'idCourse',
      as: 'typeofcourse',
    );
  ;
  return Course;
;

这里是 TypeOfCourse.js

module.exports = (sequelize, Sequelize) => 
  const TypeOfCourse = sequelize.define(
    'typeofcourse',
    
      idTypeOfCourse: 
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        field: 'idtypeofcourse',
      ,
      nameOfType: 
        type: Sequelize.STRING,
        field: 'nameoftype',
      ,
      language: 
        type: Sequelize.STRING,
        field: 'language',
      ,
      tags: 
        type: Sequelize.ARRAY(Sequelize.TEXT),
        field: 'tags',
      ,
      isDeleted: 
        type: Sequelize.BOOLEAN,
        field: 'isdeleted',
      ,
    ,
    
      freezeTableName: true,

      timestamps: false,

      createdAt: false,

      updatedAt: false,
    
  );
  TypeOfCourse.associate = function (models) 
    models.typeofcourse.hasMany(models.course, 
      foreignKey: 'idTypeOfCourse',
      as: 'course',
    );
  ;
  return TypeOfCourse;
;

在 course.controller.js 中,我定义了 findAll Course 函数并希望显示有关 typeOfCourse 的信息

exports.findAll = (req, res) => 
  Course.findAll(
    include: [
      
        model: TypeOfCourse,
        as: 'typeofcourse',
        through:  attributes: [] ,
      ,
    ],
  )
    .then(data => 
      res.send(data);
    )
    .catch(err => 
      res.status(500).send(
        message: err.message || 'Some error occurred while retrieving course.',
      );
    );
;

但是当我发送请求获取课程时。此消息回复:


    "message": "typeofcourse is not associated to course!"

我错在哪里以及如何解决此错误。非常感谢。

【问题讨论】:

【参考方案1】:

您可以使用hasMany 关联,它将创建一个与连接表的 N:M 关联

Course.hasMany(TypeOfCourse, 
          foreignKey:  name: 'idCourse' ,
          targetKey: 'idCourse',
          constraints: false,
          as: 'typeOfCourses',
        ),
    ```

【讨论】:

以上是关于Sequelize:“模型 A 与模型 B 没有关联”的主要内容,如果未能解决你的问题,请参考以下文章

同步 sequelize 模型:sequelize.import() 不是函数(并且已弃用)

Sequelize 和 MySQL 对照Sequelize 和 MySQL 对照

不创建带有 Sequelize 的外键

sequelize 跟 原生sql相比 会太多的影响性能么

sequelize/sequelize-typescript - 带有 HasMany 的 findAll 返回一个对象而不是数组

Sequelize.js 外键