Gery Ruslandi
Gery Ruslandi

Reputation: 109

NodeJS import each other file when stating model of Sequelize 6 Many To Many associations

I have a problem with sequelize 6 models definition.

I have a requirement to create a model with many to many association which is Product to Category.

One product can have multiple categories and one category can have multiple products. Because of this i create one pivot table called category_products.

The way i do to create a model is one file for each model. so there will be three files for each models which is Product, Category, and CategoryProduct.

The problem that i'm facing happening when i define the associations for each product and category model. Its happen because on product model file (which is product.model.js) is importing category model file (which is category.model.js) for defining many to many associations with product. Category model and its file is doing the same too, importing product.model.js for defining many to many associations with product.

Because of this my code returning

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

this error happen because on the runtime, the model variable that im importing for define the many to many association is not returning the model, but 'undefined' instead.

Here the code to make you more understand with what im facing:

// category_product.model.js

const CategoryProduct = sequelize.define('CategoryProduct', {}, {
    tableName : 'category_products',
    underscored: true,
});

export default CategoryProduct;

.

// product.model.js

import Category from "./category.model";
import CategoryProduct from "./category_product.model";

const Product = sequelize.define('Product', {}, {
    tableName : 'products',
    underscored: true,
});

Product.belongsToMany(Category, {
    through: CategoryProduct,
    foreignKey: 'product_id',
    otherKey: 'category_id'
})

export default Product;

.

// category.model.js

import Product from "./product.model";
import CategoryProduct from "./category_product.model";

const Category = sequelize.define('Category', {}, {
    tableName : 'categories',
    underscored: true,
});

// here the problem. im using babel,  'Product' variable is returning undefined
// the hypothesis that came to my mind why this is happening because 
// this file (category.model.js) is including product.model.js
// and product.model.js is including this file too.
// this is lead to infinity cycle of file include. and because of this,
// babel returning 'undefined' instead
// and this issue is happening to product.model.js too

Category.belongsToMany(Product, {
  through: CategoryProduct,
  foreignKey: 'category_id',
});

export default Category;

i know that i can solve this issue by creating an additional file and put association definition for each model there, but i don't want this way. I want to keep with one model for one file. Is there a solution for me? Thank you anyone.

Upvotes: 0

Views: 455

Answers (1)

Anatoly
Anatoly

Reputation: 22783

You should define associations as functions that can be called separately after all models will be registered. See my answer

Upvotes: 2

Related Questions