Search code examples
node.jsexpresssequelize.js

Sequelize circular dependencies on defining associations: belongsToMany called with something that's not a subclass of Sequelize.Model


I am trying to define many-to-many associations between two models: BlogPost and Category. I have defined the models as per the official documentation, however when I run my app, I get this error when sequelize.sync() is called:

/Users/muoki/Sites/node_blog/node_modules/sequelize/lib/associations/mixin.js:33
      throw new Error(`${this.name}.belongsToMany called with something that's not a subclass of Sequelize.Model`);
      ^

Error: category.belongsToMany called with something that's not a subclass of Sequelize.Model
    at Function.belongsToMany (/Users/muoki/Sites/node_blog/node_modules/sequelize/lib/associations/mixin.js:33:13)
    at Object.<anonymous> (/Users/muoki/Sites/node_blog/models/Category.js:20:10)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/muoki/Sites/node_blog/models/BlogPost.js:4:18)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
[nodemon] app crashed - waiting for file changes before starting...

When I remove once association from either models, the db syncs successfully and all tables are created including the pivot table blog_category What could I be missing?

Here is my BlogPost.js:

const Sequelize = require("sequelize");
const sequelize = require("../util/database");
const User = require("./User");
const Category = require("./Category");

const BlogPost = sequelize.define(
  "blog_post",
  {
    id: {
      primaryKey: true,
      autoIncrement: true,
      type: Sequelize.DataTypes.BIGINT,
    },
    image_url: {
      type: Sequelize.DataTypes.TEXT,
    },
    title: {
      type: Sequelize.DataTypes.STRING,
      allowNull: false,
    },
    content: {
      type: Sequelize.DataTypes.TEXT,
      allowNull: false,
    },
  },
  {
    timestamps: true,
    createdAt: "created_at",
    updatedAt: "updated_at",
  }
);



BlogPost.belongsTo(User);

BlogPost.belongsToMany(Category, {
  through: "blog_category",
});

module.exports = BlogPost;

And here is my Category.js

const Sequelize = require("sequelize");
const sequelize = require("../util/database");
const BlogPost = require("./BlogPost");


const Category = sequelize.define("category", {
  id: {
    type: Sequelize.DataTypes.BIGINT,
    notNull: true,
    primaryKey: true,
    autoIncrement: true,
  },
  category: {
    type: Sequelize.DataTypes.STRING,
    notNull: true,
  },
});


Category.belongsToMany(BlogPost, {
  through: "blog_category",
});

module.exports = Category;

Solution

  • You simply need to import all models in another module and below have all associations defined (something like db.js where you create Sequelize instance and call sync at the end).

    Look at my other answer here to get the idea