Some Sequelize users of SQL databases often use sync({ force: true })
in the early days, and then switch over to migrations. I'm confused because the migrations and models seem to have a similar syntax, but also a number of differences that don't seem to be strongly highlighted.
Some observations (which may be wrong):
- the options objects in the two are different:
options.indexes
object in the model doesn't work in migrations (but works in models) - Custom index names using
unique: 'somename'
doesn't work in the migration field attributes (but works in models); but aunique: true
works in both - foreign keys need to be explicitly created - and a
references
needs to be set in migrations (but in models, the hasMany/belongsTo automatically generates the foreign key field and foreign key reference) id
,createdAt
andupdatedAt
must be manually created in a migration, but not a model
The last two make sense as it's delineating the migration responsibilities (which sets up the db schema) versus the model - but what the migration supports/how it works seems different for similar items, and the sync
option exacerbates my confusion.
Some questions:
- What are the main differences to keep in mind between migrations and models in Sequelize (not the general philosophy between the two)? What options in models (when using the
sync
option) do work in migrations? - Do others simply copy model files into migrations when switching from
sync
to discrete migrations? Do you cut out parts of the extraneous info that only works in models? Only works in migrations?