Unhandled rejection Error: User (friends) is not associated to SNS!
See original GitHub issueI’m trying to design data model for represent network for user and user’s friends
basically, there are two model. User for store user’s own information. and SNS for store user’s relationship.
here is my
var User = sequelize.define(
'User', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: DataTypes.STRING,
email: DataTypes.STRING(256),
pwd: DataTypes.STRING(2048),
avatar: DataTypes.STRING(2048),
property: DataTypes.JSON,
preference: DataTypes.JSON
})
var SNS = sequelize.define("SNS", {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
status: DataTypes.INTEGER
})
here is the code i added for association models.User.belongsToMany(models.User, { as: ‘friends’, foreignKey: “userId”, through: models.SNS });
models.User.belongsToMany(models.User, {
as: 'networks',
foreignKey: "friendId",
through: models.SNS
});
when i try to cal findAll() with “include” , i got a error. (refer to code below)
SNS.findAll({
include: [{
model: models.User,
as: 'friends'
}]
})
.then(function(result) {
res.send(result);
});
here is the error Unhandled rejection Error: User (friends) is not associated to SNS!
what’s wrong with my model definition. I could a execute a query with include.
The findAll work if i remove the include part. but in that case i will get only userID in SNS model. however i want a user object as well. i don’t want to do a query again.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:6 (2 by maintainers)

Top Related StackOverflow Question
thanks Jameier, it works, thanks for help.
here is association models.SNS.belongsTo(models.User, { foreignKey: “userId” });
here is the query
You need to add
{ as: friends}so that you have to same as (or none) on both the model and the association