7

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 a unique: 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 and updatedAt 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?
user1985427
  • 353
  • 3
  • 11
  • Sync definitely makes some assumptions when creating the schema in regards to primary keys, constraints, associations and indexes. All of these assumptions/conveniences needs to be explicitly declared in the migration. – holmberd Feb 22 '18 at 17:30
  • I would also be interested in a more detailed answer. As far as I understand it, the migration file will communicate with the database and only DB related things are usable. The model on the other hand, almost acts as a middleware before sending data to database. So getters and setters won't have any affect on a migration file, but will on a model file. – gbland777 Apr 20 '20 at 22:13

0 Answers0