I have 2 table:
- User
- Follow
Table follow has 2 foreign keys:
- follower_id
- followee_id
They are both foreign key to user table
I have the following associations created:
//Users-Followers
this.follows.belongsTo(this.users, {
as: 'follower',
foreignKey: 'follower_id'
});
this.users.hasMany(this.follows, {
as: 'folower',
foreignKey: 'follower_id'
});
//Users-Followees
this.follows.belongsTo(this.users, {
as: 'folowee',
foreignKey: 'followee_id'
});
this.users.hasMany(this.follows, {
as: 'folowee',
foreignKey: 'followee_id'
});
Now I want to: get who is following who.
At the following is my query:
async function getFollowByFollower(follower_id) {
let follows = model.follows;
let users = model.users;
let follow = await follows.findAll({
include: [
{
model: users,
as: 'follower',
required: true,
where: {
id: follower_id
}
},
{
model: users,
as: 'followee',
required: true
}
]
}).catch(function(err) {
console.log('followsDAO.js getFollowByFollower error: ' + err);
});
return follow;
};
Here goes the error message:
SequelizeEagerLoadingError: users is associated to follows multiple times. To identify the correct association, you must use the 'as' keyword to specify the alias of the association you want to include.
As you can see, I have clearly indicated 'as' in both query and association.
Please help.
Thanks. Best regards,