belongsToMany Association on the same model with a different foreign key and alias fails
See original GitHub issueWhat 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:
- Created 6 years ago
- Reactions:12
- Comments:24 (2 by maintainers)
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:
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 🙂