Sequelize Eager Loading Error: social_logins not associated to users

1k views Asked by At

Users Model defined like this.

const db = require ('../../config/db_config');

const users = db.sequelize.define('users', {
  id: {
    type: db.DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
  },
  first_name: {
    type: db.DataTypes.STRING(150),
  },
  last_name: {
    type: db.DataTypes.STRING(150),
  },
  email: {
    type: db.DataTypes.STRING(256),
    required: true,
    unique: true
  },
  password: {
    type: db.DataTypes.STRING,
  },
  student_id: {
    type: db.DataTypes.STRING
  },
  status: {
    type: db.DataTypes.BOOLEAN,
    required: true,
    defaultValue: 0
  },
  is_deleted: {
    type: db.DataTypes.BOOLEAN,
    required: true,
    defaultValue: 0
  },
  createdAt: db.DataTypes.DATE,
  updatedAt: db.DataTypes.DATE,
});

module.exports = users;

social_logins Model defined like this

const db = require ('../../config/db_config');

const socialLogins = db.sequelize.define('social_logins', {
  id: {
     type: db.DataTypes.INTEGER,
     primaryKey: true,
     autoIncrement: true,
  },
  token: {
    type: db.DataTypes.STRING
  },
  tokenType: {
    type: db.DataTypes.STRING
  },
  fb_id: {
    type: db.DataTypes.STRING
  },
  user_id: {
    type: db.DataTypes.INTEGER
  },
  is_deleted: {
    type: db.DataTypes.BOOLEAN,
    required: true,
    defaultValue: false
  }
}, { underscored: true, timestamp: true, tableName: 'social_logins' });

module.exports = socialLogins;

User model associated with the social_logins model using belongsTo function

socialLoginsModel.belongsTo(users)

Sequelize throws eagerLoadingError Error EagerLoadingError [SequelizeEagerLoadingError]: social_logins is not associated to users!

While running this query given below.

const userModel = require ('./users_model');
const socialLoginModel = require('../social_logins/social_logins_model');

let id = "123456";
let email = "[email protected]";

userModel.findOne({
  where: { email },
  include: [{
     model: socialLogins,
     where: {
       fb_id: id
     }
  }]
});
1

There are 1 answers

0
rohan dani On

you should have associations like this based on your model names

socialLogins.belongsTo(users) & users.hasOne(socialLogins)/ users.hasMany(socialLogins) based on your relations being defined in DB