'SequelizeEagerLoadingError with Sequelized
I'm getting an error when trying to run a Sequelized query. The query works when I am only asking for a User, but breaks when I add 'include' and try to join with another table (the blogs table).
Here is my User model:
const { Model, DataTypes } = require('sequelize');
const sequelize = require('../config/connection');
const bcrypt = require('bcrypt');
// create our User model
class User extends Model {
checkPassword(loginPw) {
return bcrypt.compareSync(loginPw, this.password);
}
}
// define table columns and configuration
User.init(
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
username: {
type: DataTypes.STRING,
allowNull: false
},
password: {
type: DataTypes.STRING,
allowNull: false,
validate: {
// this means it must be at least 4 characters
len: [4]
}
}
},
{
//(password hashing)
hooks: {
// set up beforeCreate lifecycle 'hook'
async beforeCreate(newUserData) {
newUserData.password = await bcrypt.hash(newUserData.password, 10);
return newUserData;
},
// set up beforeUpdate lifecyle
async beforeUpdate(updatedUserData) {
updatedUserData.password = await bcrypt.hash(updatedUserData.password, 10);
return updatedUserData;
}
},
sequelize,
timestamps: false,
freezeTableName: true,
underscored: true,
modelName: 'user'
}
);
module.exports = User;
Here is my Blog Model:
const { Model, DataTypes } = require('sequelize');
const sequelize = require('../config/connection');
class Blog extends Model{};
Blog.init(
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
blog_title: {
type: DataTypes.STRING,
allowNull: false
},
blog_text: {
type: DataTypes.STRING(6000),
allowNull: false,
},
user_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'user',
key: 'id'
}
}
},
{
sequelize,
freezeTableName: true,
underscored: true,
modelName: 'blog'
}
);
module.exports = Blog;
Here are my associations inside the Index file:
const User = require('./User');
const Blog = require('./Blog');
// User can have many Blogs
User.hasMany(Blog, {
foreignKey: 'user_id'
});
Blog.belongsTo(User, {
foreignKey: 'user_id'
});
module.exports = { User, Blog }
And here are is my route. It works perfectly fine without the include option.
const router = require('express').Router();
const User = require('../../models/User');
const Blog = require('../../models/Blog');
router.get('/', (req, res) => {
User.findAll({
attributes: { exclude: ['password']},
include: [
{
model: Blog,
}
]
})
.then(dbUserData =>
res.json(dbUserData))
.catch(err => {
console.log(err);
res.status(500).json(err);
})
})
module.exports = router;
This is the error I get when I try the route:
EagerLoadingError [SequelizeEagerLoadingError]: blog is not associated to user!
Any and all help is greatly appreciated.
Solution 1:[1]
In case anyone comes across this problem.
I imported my models like this:
const User = require('../../models/User');
const Blog = require('../../models/Blog');
They should have been imported like this:
const { User, Blog } = require('../../models);
That way the models are pulled from the index file after the associations have been assigned to them.
Credit to Sandrine Poissonnet who spotted my error.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anthony Barragan |
