Open mrdulin opened 4 years ago
As a newcomer to Sequelize, this is a critically important issue with the documentation that is slowing me down more than any other single task as I spin up a new project.
I've regenerated and modified all my migrations and models twice now, inspecting the resulting schema through PG cli, just trying to figure out the "right" way.
I'm getting ready to start over on this project with a different language because of the lack of convention around migrations and models in Sequelize.
This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂
Issue Description
It's unclear about how to create associations in migration files when the models have associations with each other.
What was unclear/insufficient/not covered in the documentation
I read the migrations docs and the code of sequelize express-example.
The docs just explain how to create a model and migration file using
sequelize-cli
. The thing is when people create models and migration files such asUser
andTask
and define associations like this:Now, the models have associations. But, the migration file DOES NOT have associations. This means even if the
UserId
is added to theTask
model. But in the migration file, theTask
table doesn't haveUserId
at this moment.Here are the workflow steps I current use:
sequelize-cli
UserId
column toxxx-create-task.js
file (This is where I'm not sure and confused. But I saw the express-example use this workflow)So, what's the correct workflow to create associations in migration files?
If possible: Provide some suggestion on how we can enhance the docs
If I understand correctly, the migration file and the model are a different part. This means we have to keep both in sync manually, sequelize and sequelize-cli will not do it for us automatically.
Another issue is to create the join table,
sequelize-cli
will not create the join table, right? If so, what's the best way to create this join table migration file?It's different from the
sequelize.sync()
API. For testing purposes, this API will create tables with correct associations, addUserId
column to theTask
table based on the models' associations we defined. Besides, it also create join table for many-to-many association too. It seems this API will keep the model and "migration" sync automaticallyAdditional context
I found a post about this in medium: https://medium.com/@andrewoons/how-to-define-sequelize-associations-using-migrations-de4333bf75a7
It seems the author uses the same workflow as mine
Issue Template Checklist
Is this issue dialect-specific?
Would you be willing to resolve this issue by submitting a Pull Request?