.hasMany called with something that's not a subclass of Sequelize.Model
Solution 1
If this is many to many scenario, it's better to create a new model and in that model, you can add the relationship. Otherwise, put the relation in One side.
Admin.belongsTo(User);
and also in admin side you can put
User.hasMany(Admin);
Solution 2
In my case, the error was caused by me saving different models in different .js files. I was then requiring all of them in a single initialize.js file and then defining the associations, such as hasMany and belongsTo. This was apparently causing a problem.
I moved all my Models to a single file and described the associations in the same file and it worked.
Solution 3
I think you are trying to maintain a foreign key in user_relation model for the userid key in Admin model. If that is so, you have to define sourceKey and targetKey in association.
Define both sourceKey and targetKey with same datatype.
In Admin model
Admin.belongsTo(user_relation, {
foreignKey: 'userid',
targetKey: 'userid'
});
In user_relation model
user_relation.hasMany(Admin, {
as: 'admin',
foreignKey: 'userid',
sourceKey: 'userid'
});
Related videos on Youtube
user7555459
Updated on June 04, 2022Comments
-
user7555459 almost 2 years
I'm trying to refer a foreign key between 2 models. but I'm getting this error:
throw new Error(this.name + '.hasMany called with something that\'s not a subclass of Sequelize.Model');
^Error: user_relation.hasMany called with something that's not a subclass of Sequelize.Model
squelize.js
const Sequelize = require('sequelize'); const config = require('./default.config') const sequelize = new Sequelize(config.database, config.user, config.password, { host: config.host, port: config.port, dialect: 'mysql', timezone: config.timezone,//东八区 pool: { max: 5, min: 0, acquire: 30000, idle: 10000 } }); sequelize .sync() .then(err => { console.log('Connection has been established successfully.'); }) .catch(err => { console.error('Unable to connect to the database:', err); }); module.exports = sequelize;
User model
const sequelize = require('sequelize'); const Model = require('../../config/squelize');
const Admin = Model.define('admin', { username : {type : sequelize.STRING, allowNull : false},//用户名 password : {type : sequelize.STRING, allowNull : false},//密码 details : {type : sequelize.STRING, allowNull : true},//简介 head_thumb : {type : sequelize.STRING, allowNull : true},//头像 gender : {type : sequelize.STRING, allowNull : true},//性别 nickname : {type : sequelize.STRING, allowNull : true},//昵称 userid : {type : sequelize.INTEGER, autoIncrement : true, primaryKey : true}//用户userid }, { freezeTableName:true }) module.exports = Admin;
admin_relation.js
var Sequelize = require('sequelize'); const Model = require('../../config/squelize'); const Admin = require('./admin.model') var user_relation = Model.define('user_relation', { id : {type : Sequelize.INTEGER, autoIncrement : true, primaryKey : true}, userid : {type : Sequelize.STRING, allowNull : false},//用户id frendid : {type : Sequelize.STRING, allowNull : false}//朋友id },{ timestamps:false, freezeTableName:true, }); user_relation.hasMany(Admin,{as:'admin',foreignKey:'userid'}) module.exports = user_relation;
Does someone have already see an error that look like that ? I search for few days without any issue, if someone could help I'll really appreciate,
thank !
-
Mousam Singh over 2 yearsThis does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question.
-
Amon over 2 yearsThis worked for me, apparently it has something to do with the order the files are loaded in