Problem when creating Many to Many association using Sequelize (belongsToMany)

Asked

Viewed 226 times

0

I am getting the following error when defining a Many to Many association between two tables:

C:\eadfabet\node_modules\sequelize\lib\associations\mixin.js:49 throw new Error(`${this.name}.belongsToMany called with something that's not a subclass of Sequelize.Model`);

Error: Teacher.belongsToMany called with something that's not a subclass of Sequelize.Model
    at Function.belongsToMany (C:\eadfabet\node_modules\sequelize\lib\associations\mixin.js:49:13)
    at Function.associate (C:\eadfabet\src\app\models\Teacher.js:33:10)
    at C:\eadfabet\src\database\index.js:26:45
    at Array.map (<anonymous>)
    at Database.init (C:\eadfabet\src\database\index.js:25:8)
    at new Database (C:\eadfabet\src\database\index.js:17:10)
    at Object.<anonymous> (C:\eadfabet\src\database\index.js:31:20) ...

I will explain my scenario, I have two tabels, Teachers and Courses, their association is Many to Many. To create the association, I created a "pivot" table called courses_teachers, follows the Migration:

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('courses_teachers', {
      id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
      },
      course_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'courses', key: 'id' },
        onDelete: 'CASCADE',
        onUpdate: 'CASCADE',
      },
      teacher_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'teachers', key: 'id' },
        onDelete: 'CASCADE',
        onUpdate: 'CASCADE',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
    });
  },

  down: (queryInterface) => {
    return queryInterface.dropTable('courses_teachers');
  },
};

I defined the association in the Courses model, I did not get any error in the terminal:

import Sequelize, { Model } from 'sequelize';

class Courses extends Model {
  static init(sequelize) {
    super.init(
      {
        name: Sequelize.STRING,
      },
      {
        sequelize,
      }
    );
    return this;
  }

  static associate(models) {
    this.belongsToMany(models.Teacher, {
      foreignKey: 'course_id',
      through: 'courses_teachers',
      as: 'teachers',
    });
  }
}

export default Courses; 

However, when I defined the association in the Teachers model, I obtained the error reported above. Model of Teachers:

import Sequelize, { Model } from 'sequelize';

class Teacher extends Model {
  static init(sequelize) {
    super.init(
      {
        name: Sequelize.STRING,
      },
      {
        sequelize,
      }
    );

    return this;
  }

  static associate(models) {
    this.belongsToMany(models.Course, {
      foreignKey: 'teacher_id',
      through: 'courses_teachers',
      as: 'courses',
    });
  }
}

export default Teacher;

I’ve been racking my brain all day trying to figure out what might be causing this mistake.

  • I found the problem, I was referring to the Course model incorrectly, the correct is Courses.

1 answer

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.