Search code examples
node.jspostgresqlsequelize.jsassociationshas-and-belongs-to-many

Problem creating many to many association in sequelize. (belongsToMany)


I am getting the following error when I define a many to many association between two tables:

Error: Teacher.belongsToMany called with something that's not a subclass of Sequelize.Model

I will explain my scenario.

I have the teacher and course tables, to create the association, I created a pivot table called courses_teachers.

Migration code for courses_teachers:

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');
  },
};

Models:

Teacher:

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;

Course:

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; 

Error:

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) ...

[nodemon] app crashed - waiting for file changes before starting... 

The error only happens in the model teacher, in the model course it doesn't happen.


Solution

  • I was referring to the Model Course incorrectly.

    Thank you @William Prigol Lopes !