51

Im learning Sequelize and I'd like some clarification around syncing vs migrations.

I understand that sync will create missing tables based on my model schema but I have also read that sync is meant for initializing the database whereas migrations are meant for production.

If that is the case, the express-example shows calling sync from bin/www. Is that something that should not be used in production?

As an extension of this, if I am not to use sync in production, how do you apply model associations? Do I need to add them to migrations manually?

Essentially I am asking for an explanation of how these two concepts are meant to work together.

Thanks

adampetrie
  • 1,130
  • 1
  • 11
  • 23

1 Answers1

44

I recommend using sequelize migrations in development and production so that you are fully acclimate with the process which will give safe results, also sequelize sync without force will only create new tables with the specified schema which are not present in database, it wont reflect alterations in existing table schema. Sequelize migrations will help you update your database in a systematic and incremental manner.

Refer this page for more on this.

Sequelize.js: how to use migrations and sync

http://corpus.hubwiz.com/2/node.js/21105748.html

Keval
  • 3,246
  • 20
  • 28
  • 1
    Thanks for the answer. That article is a good resource. Am I to presume then that adding `underscored: true` or `onDelete: 'cascade'` to the model's JS definition (not the migration) will have no affect if sync is not called? – adampetrie Jan 15 '17 at 14:34
  • 3
    Just discovered that your link is a scraped version of this SO question: http://stackoverflow.com/questions/21105748/sequelize-js-how-to-use-migrations-and-sync The SO version has more information so maybe change your link to the SO instead. – adampetrie Jan 15 '17 at 14:38