I get an issue using relations in SequelizeJS.
My error message : (500 : Internal Server Error) user is not associated to article!
Article model :
var Article = db.define('article', {
title: {
type: Types.STRING,
},
content: {
type: Types.TEXT,
},
},{
classMethods:{
associate: function (models) {
Article.belongsTo(models.User, {as: 'Author'});
}
}
});
User model :
var User = db.define('user', {
username: {
type: Types.STRING,
},
email: {
type: Types.STRING,
},
password: {
type: Types.STRING
},
},{
classMethods:{
associate: function (models) {
User.hasMany(models.Article, {as: 'Articles'});
}
}
});
And my include query :
getAll: function (callback) {
return this.context.Article.findAll({include: [this.context.User]});
},
Article.findAll({include: [{ model: this.context.User, as: 'Author'}]});
Because the relation has an alias (author), you have to provide that in eager loading as well