sequelize-auto icon indicating copy to clipboard operation
sequelize-auto copied to clipboard

Create a through relationship

Open shuat opened this issue 4 years ago • 12 comments

This is a great project and would love to be able to use it without having to modify the init-models file.

I am having an issue when trying to add a thorough relationship such as below I get an error since the generator already made a relationship.

usersExtended.belongsToMany(roles, {
  through: user_roles,
  as: "user_roles_through",
});

Error is

SequelizeAssociationError: You have used the alias user_roles in two separate associations. Aliased associations must have unique aliases.

Is there any way to get this to work? One way would be to not name them as the same table name.

shuat avatar Dec 02 '21 04:12 shuat

I'm not sure that I understand the problem.

The init-models creates a relationship, and you are creating the same relationship?

Or you are creating a different relationship, but you want to use the same name that init-models used? Why do you want to use the same name?

steveschmitt avatar Dec 02 '21 18:12 steveschmitt

@steveschmitt I'm having the same issue:

Different relationship, i don't care for the same name it is happening automatically even if i specify an "as" on the through relationship...

i'm trying to create a "through" relationship from the "users" table to the "roles" table through the "user_roles" table and it says the the association "user_roles" already exists.

DecentGradient avatar Dec 02 '21 22:12 DecentGradient

Hmm. Sounds like a sequelize bug.

Does it work if both belongsToMany relationships specify an "as" that is not the table name?

steveschmitt avatar Dec 02 '21 22:12 steveschmitt

No, it seems to ignore the "as"

On Thu, Dec 2, 2021, 5:44 PM Steve Schmitt @.***> wrote:

Hmm. Sounds like a sequelize bug.

Does it work if both belongsToMany relationships specify an "as" that is not the table name?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/sequelize/sequelize-auto/issues/574#issuecomment-985061749, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABDGK4FKAUGJV44AOYYMMGTUO7ZD5ANCNFSM5JGES4QA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

DecentGradient avatar Dec 02 '21 22:12 DecentGradient

Then I don't know how we can fix this.

steveschmitt avatar Dec 02 '21 22:12 steveschmitt

Even though it sounds like it's not an error in your package, it still might be helpful if we had the option of not providing aliases for the models that are generated as a workaround to this issue and also a feature . What do you think about that?

DecentGradient avatar Dec 03 '21 13:12 DecentGradient

So, if no alias is provided, then you can create two belongsToMany relationships?

steveschmitt avatar Dec 03 '21 16:12 steveschmitt

Yes if I understand correctly

On Fri, Dec 3, 2021, 11:50 AM Steve Schmitt @.***> wrote:

So, if no alias is provided, then you can create two belongsToMany relationships?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/sequelize/sequelize-auto/issues/574#issuecomment-985672259, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABDGK4CPJI3IHSLP6K6DFK3UPDYOJANCNFSM5JGES4QA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

DecentGradient avatar Dec 03 '21 17:12 DecentGradient

Please confirm by hacking up the sequelize-auto output until it works. Then post it here so I can see what the desired output should be. Thanks!

steveschmitt avatar Dec 03 '21 18:12 steveschmitt

Great idea, would you be able to point me to where in the sequalize auto code that would be?

On Fri, Dec 3, 2021, 1:05 PM Steve Schmitt @.***> wrote:

Please confirm by hacking up the sequelize-auto output until it works. Then post it here so I can see what the desired output should be. Thanks!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/sequelize/sequelize-auto/issues/574#issuecomment-985723233, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABDGK4FQQA64FFZ6LR4XVLLUPEBIJANCNFSM5JGES4QA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

DecentGradient avatar Dec 04 '21 23:12 DecentGradient

What I meant was, generate your model using the normal sequelize-auto, then edit those output files (such as init-models) until they work for you. Then post the working output files here.

steveschmitt avatar Dec 05 '21 22:12 steveschmitt

Ok, thanks for the clarification.

On Sun, Dec 5, 2021, 5:14 PM Steve Schmitt @.***> wrote:

What I meant was, generate your model using the normal sequelize-auto, then edit those output files (such as init-models) until they work for you. Then post the working output files here.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/sequelize/sequelize-auto/issues/574#issuecomment-986310195, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABDGK4D5NE3CGOPDB446HRLUPPP6DANCNFSM5JGES4QA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

DecentGradient avatar Dec 05 '21 22:12 DecentGradient