sequelize / express-example

A proposal for the usage of Sequelize within an Express.JS application.
MIT License
2.5k stars 770 forks source link

duplicate foreignKey when using sequelize.sync() #90

Closed tonghoai closed 4 years ago

tonghoai commented 5 years ago

I'm using models like files in models folder. But when i'm using sequelize.sync() some models duplicated foreginKey like following

image

This is my associate

image

image

image

image

Have 3 foreginKey but sequelize has created 6 foreginKey. Anybody can help me? Thank you!

remjx commented 5 years ago

Each belongsTo and hasMany creates one foreign key. It may make sense to use both in situations like this so you can run joins both ways Sequelize querying syntax. If you'll only need to join A to B and not B to A, you don't need both. At least that is my understanding.

On Tue, Mar 26, 2019, 12:38 AM tonghoai notifications@github.com wrote:

I'm using models like files in models folder. But when i'm using sequelize.sync() some models duplicated foreginKey like following

[image: image] https://user-images.githubusercontent.com/10321465/54971858-187f8600-4fbb-11e9-8d9f-6607319cda16.png

This is my associate

[image: image] https://user-images.githubusercontent.com/10321465/54971889-449b0700-4fbb-11e9-83b0-ed1e199f0ff8.png

[image: image] https://user-images.githubusercontent.com/10321465/54971899-5ed4e500-4fbb-11e9-9ad1-5b56667575a3.png

[image: image] https://user-images.githubusercontent.com/10321465/54971908-6a281080-4fbb-11e9-8964-568f210ab9ed.png

[image: image] https://user-images.githubusercontent.com/10321465/54971916-7613d280-4fbb-11e9-9051-f09b6b9a49d6.png

Have 3 foreginKey but sequelize has created 6 foreginKey. Anybody can help me? Thank you!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/sequelize/express-example/issues/90, or mute the thread https://github.com/notifications/unsubscribe-auth/AhfqFUzgJJ60L4wsFytTw1IH2WUROsp7ks5vaaRBgaJpZM4cKlYw .

tonghoai commented 5 years ago

Each belongsTo and hasMany creates one foreign key. It may make sense to use both in situations like this so you can run joins both ways Sequelize querying syntax. If you'll only need to join A to B and not B to A, you don't need both. At least that is my

I tried removing belongsTo and it works, but duplicator only appears in some models, awesome!

papb commented 4 years ago

Hello @tonghoai, are you still experiencing this issue with duplicate foreign keys? If yes, I would like to ask you to open an issue in the main repo.

I will close this issue for now, but let me know if you think it should be reopened.