question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

belongsToMany Association on the same model with a different foreign key and alias fails

See original GitHub issue

What you are doing?

I have defined my association on the User model as follows

    User.belongsToMany(User, {
      through: models.Following,
      as: 'Follower',
      foreignKey: 'following_id',
    });

    User.belongsToMany(User, {
      through: models.Following,
      as: 'Following',
      foreignKey: 'follower_id',
    });

What do you expect to happen?

I did this so that I could then call user.GetFollower() and user.GetFollowing() functions. This worked fine with sequelize v3

What is actually happening?

With sequelize v4, I now get the error SequelizeAssociationError: You have used the alias Following in two separate associations. Aliased associations must have unique aliases.

If I remove the second User.belongsToMany, it solves the problem but I am no longer able to use user.getFollowing() which returns as undefined.

Dialect: mysql Database version: 5.7 Sequelize version: 4.8

P.S. I did check https://github.com/sequelize/sequelize/issues/7775. But it is not applicable to my case. I do not have the Model.associate(models) being called multiple times. I have it called only once - in the index.js file that sequelize-cli generated.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:12
  • Comments:24 (2 by maintainers)

github_iconTop GitHub Comments

17reactions
chornbecommented, Jul 26, 2018

That… that is not good. The longer I use it, the more I think I’m just going to write my queries as needed, and start moving away from Sequelize as a data object layer. Which stinks; in its basic form, a couple versions old, it was a great way to get basic, simple-data-model with basic relations working quickly and clearly. There’s just too much finicky tweaking the last year or two for my liking. 😦

Chris Hornberger chris.hornberger@gmail.com

On Thu, Jul 26, 2018 at 9:38 AM, Bogdan Polovko notifications@github.com wrote:

I’m trying to update to Sequelize 4. In my case I have two models using the same alias. That’s how front-end expects it to be:

Clinic.belongsTo(Location, { as: ‘location’, foreignKey: ‘locationId’ });Location.hasOne(Clinic, { as: ‘location’, foreignKey: ‘locationId’ }); Company.belongsTo(Location, { as: ‘location’, foreignKey: ‘locationId’ });Location.hasOne(Company, { as: ‘location’, foreignKey: ‘locationId’ });

Why should this throw an error, what’s wrong with using two same aliases on two different models? Is there a way to disable it? Otherwise all our front-end applications will have to be updated, which is insane amount of work.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/sequelize/sequelize/issues/8263#issuecomment-408100247, or mute the thread https://github.com/notifications/unsubscribe-auth/AHJ5ebUajwAF7LgN1bOZnTC7woS3_VWSks5uKcZPgaJpZM4POm7F .

14reactions
stale[bot]commented, Apr 26, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

Associations
The A.hasOne(B) association means that a One-To-One relationship exists between A and B , with the foreign key being defined in the target...
Read more >
How to disambiguate between multiple associations ...
I use as which allows you to reference the relationship through that alias. Institution.belongsToMany(models.
Read more >
Associations - Sequelize
One -To-One associations are associations between exactly two models connected by a single foreign key. BelongsTo. BelongsTo associations are associations where ...
Read more >
AssociationOptionsBelongsToMa...
The alias of this model, in singular form. See also the name option passed to sequelize.define . If you create multiple associations between...
Read more >
Sequelize associations: 1:1 mapping - Siddharth Lakhara
Whenever the foreign key is defined on the source model (the model for which we are writing this association), use belongsTo or belongsToMany...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found