hello my Address model
const { DataTypes } = require('sequelize');
const sequelize = require('../../../db/connection');
var BorrowerInfo = require('../../models/borrowerInfo/BorrowerInfo')
const Address = sequelize.define('addresses', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false,
},
street: {
type: DataTypes.STRING,
allowNull: false
},
unit: {
type: DataTypes.STRING,
allowNull: false
},
city: {
type: DataTypes.STRING,
allowNull: false
},
state: {
type: DataTypes.STRING,
allowNull: false
},
zip: {
type: DataTypes.STRING,
allowNull: false
},
country: {
type: DataTypes.STRING,
allowNull: false
},
},
{
freezeTableName: true
},
{
}
);
Address.associate = (models) => {
Address.belongsToMany(BorrowerInfo, {
through: 'BorrowerAddresses',
as: 'address',
foreignKey: 'addressId'
});
};
module.exports = Address;
My borrower moddel
const { DataTypes } = require('sequelize');
const sequelize = require('../../../db/connection');
const Address = require('../../models/common/Address')
const BorrowerInfo = sequelize.define('borrower_info', {
id : {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true ,
allowNull: false,
},
loanAppNo : {
type: DataTypes.STRING,
allowNull: false
},
firstName: {
type: DataTypes.STRING,
allowNull: false
},
lastName: {
type: DataTypes.STRING,
allowNull: false
},
middleName:{
type: DataTypes.STRING,
allowNull: false
},
suffix: {
type: DataTypes.STRING,
allowNull: false
},
ssn: {
type: DataTypes.STRING,
allowNull: false
},
dob: {
type: DataTypes.DATE,
allowNull: false
},
citizenship: {
type: DataTypes.STRING,
allowNull: false
},
maritalStatus: {
type: DataTypes.ENUM('1','2','3'),
},
noOfDependantChilds: {
type: DataTypes.INTEGER,
allowNull: false
},
agesDependantChilds: {
type: DataTypes.STRING,
allowNull: false
},
contactInfo: {
type: DataTypes.JSON,
allowNull: false
},
createdAt: {
type: DataTypes.NOW,
}
},
{
timestamps: false,
freezeTableName: true
},
{
}
);
BorrowerInfo.belongsToMany(Address, {
through: 'borrower_addresses',
});
module.exports = BorrowerInfo;
my borrower address model
const {
DataTypes
} = require('sequelize');
var Addresses = require('../common/Address')
const sequelize = require('../../../db/connection');
const BorrowerAddresses = sequelize.define('borrower_addresses', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false,
},
borrowerInfoId: {
type: DataTypes.INTEGER,
allowNull: false
},
type: {
type: DataTypes.ENUM('current', 'former', 'mailing'),
allowNull: false
},
addressId: {
type: DataTypes.INTEGER,
allowNull: false
},
howLongAtCurrentAddress: {
type: DataTypes.INTEGER,
allowNull: false
},
housing: {
type: DataTypes.ENUM('1', '2', '3'),
},
rentPerMonth: {
type: DataTypes.FLOAT,
allowNull: false
},
createdAt: {
type: DataTypes.NOW,
}
}, {
timestamps: false,
freezeTableName: true
});
module.exports = BorrowerAddresses;
error: borrower_info.belongsToMany called with something that's not a subclass of Sequelize.Model
when i am consoling address model inside borrower info model getting below
BorrowerInfo: class extends Model {} Address: {}
address model is not extending the model class
but when i am consoling borrower info model inside address model it is extending the model class what's the issue please guide how can i get rid of this error
please tag some one who can help
Your model definition is the actual model name. So, make model name same as the definition like below.
module.exports = (sequelize, DataTypes) => {
const BorrowerInfo = sequelize.define('BorrowerInfo', {
id : {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true ,
allowNull: false,
},
....
{
tableName: 'borrower_info', // you can pass your tablename here
});
return BorrowerInfo;
};
If your model name is Address
, sequelize will map this model to a table named Addresses
, you can prevent that behavior by passing table name explicitly.
I found solution if anyone stuck here
Actually i had to initialize all models in single file than have to import it from that single file for eg.
file: index.js
'use strict';
const fs = require('fs');
const path = require('path');
const basename = path.basename(module.filename);
const sequelize = require('../../db/connection');
let db = {};
// borrower info
fs.readdirSync(`${__dirname}/borrowerInfo`)
.filter(function (file) {
return (file.indexOf('.') !== 0) && (file !== basename);
})
.forEach(function (file) {
if (file.slice(-3) !== '.js') return;
let model = sequelize['import'](path.join(`${__dirname}/borrowerInfo`, file));
db[model.name] = model;
});
//loan team
fs.readdirSync(`${__dirname}/loanTeam`)
.filter(function (file) {
return (file.indexOf('.') !== 0) && (file !== basename);
})
.forEach(function (file) {
if (file.slice(-3) !== '.js') return;
let model = sequelize['import'](path.join(`${__dirname}/loanTeam`, file));
db[model.name] = model;
});
// common
fs.readdirSync(`${__dirname}/common`)
.filter(function (file) {
return (file.indexOf('.') !== 0) && (file !== basename);
})
.forEach(function (file) {
if (file.slice(-3) !== '.js') return;
let model = sequelize['import'](path.join(`${__dirname}/common`, file));
db[model.name] = model;
});
// role
fs.readdirSync(`${__dirname}/role`)
.filter(function (file) {
return (file.indexOf('.') !== 0) && (file !== basename);
})
.forEach(function (file) {
if (file.slice(-3) !== '.js') return;
let model = sequelize['import'](path.join(`${__dirname}/role`, file));
db[model.name] = model;
});
// user
fs.readdirSync(`${__dirname}/user`)
.filter(function (file) {
return (file.indexOf('.') !== 0) && (file !== basename);
})
.forEach(function (file) {
if (file.slice(-3) !== '.js') return;
let model = sequelize['import'](path.join(`${__dirname}/user`, file));
db[model.name] = model;
});
Object.keys(db).forEach(function (modelName) {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
module.exports = db;
importing in other file
const models = require('../models/index')
const exampleTable = models.tableName
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.