Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 !

like image 271
user7555459 Avatar asked May 31 '18 02:05

user7555459


3 Answers

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);
like image 72
Argon Avatar answered Sep 29 '22 20:09

Argon


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'
});
like image 45
Praveenkumar Kalidass Avatar answered Sep 29 '22 19:09

Praveenkumar Kalidass


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.

like image 42
Rohan Hussain Avatar answered Sep 29 '22 19:09

Rohan Hussain