1

I have 2 table:

  1. User
  2. Follow

Table follow has 2 foreign keys:

  1. follower_id
  2. 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,

1 Answer 1

0

Never mind. My code logic is 100% correct. The problem is in the association statement I misspelled "follower" as "folower" and "followee" as "folowee".

After correcting the spelling the code is running.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.