im trying to define sequelize association but get error when defining hasMany relationship
error throw new Error(${this.name}.hasMany called with something that's not a subclass of Sequelize.Model);
its one to many relationship between account and account_type where
account_type has many account
account belongs to account_type
here's my code
models/account.js
const Sequelize = require('sequelize');
const sequelize = require('../../helper/dbConnection');
const accountType = require('./accountType');
const account = sequelize.define('account', {
id: {
primaryKey: true,
allowNull: true,
autoIncrement: true,
type: Sequelize. INTEGER,
},
account: {
allowNull: false,
unique: true,
type: Sequelize.STRING
},
nominal: {
allowNull: false,
type: Sequelize.DOUBLE,
},
description: {
allowNull: true,
type: Sequelize.TEXT,
},
type_id: {
allowNull: false,
type: Sequelize.INTEGER,
references: {
model: 'account_type',
key: 'id'
},
onUpdate: 'cascade',
onDelete: 'cascade'
},
is_deleted: {
allowNull: false,
default: false,
type: Sequelize.BOOLEAN,
},
created_at: Sequelize.DATE,
updated_at: Sequelize.DATE,
});
account.belongsTo(accountType, { foreignKey: 'type_id' });
module.exports = account;
models/accountType.js
const Sequelize = require('sequelize');
const sequelize = require('../../helper/dbConnection');
const account = require('./account');
const accountType = sequelize.define('account_type', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
type: {
allowNull: false,
unique: true,
type: Sequelize.STRING,
},
description: {
allowNull: true,
type: Sequelize.TEXT
},
parent_id: {
allowNull: true,
type: Sequelize.INTEGER,
references: {
model: 'account_type',
key: 'id'
},
onUpdate: 'cascade',
onDelete: 'cascade'
},
created_at: Sequelize.DATE,
updated_at: Sequelize.DATE,
});
accountType.hasMany(account);
module.exports = accountType;
im already trying follow the documentation but get error instead, im really stuck, please help me anyone
thanks to sir ankh, this code is have circular dependency problem
the solution is just define the association in 1 file, either in models/account.js
or models/accountType.js