How to modify the structure of a table with sequelize without deleting the data?


I have a model made with sequelize , I can not delete the data from the institution table, but now I have to add a day field.

I've already tried sequelize.sync but it does not modify the structure of the table. and sequelize.sync ({force: true}) would erase all the data.

It is important that you do not erase anything since it is a database that as the cresca project should be modified.

asked by Johnny Pachecp 11.07.2017 в 18:10

2 answers


Using migrations is the most recommended way if you are in production. To perform a migration you simply need to write a file that exports an object with two functions:

  • up: transforms the current state of the models.
  • down: reverses some specific functionality.

Note: This file must be in the migrations directory in the root of the project.

These functions receive as an parameter an object of type QueryInterface and an instance of Sequelize. To make any kind of change you are interested in doing it in the up function.

To change a column type, just use the changeColumn method:

module.exports = {
  up(queryInterface, sequelize) {
    return queryInterface.changeColumn('nombreModelo', 'nombreAtributo', {
      type: sequelize.<nuevo tipo>,
      // otros cambios

Finally you must use the CLI to run your migration:


Note: Install the package sequelize-cli if you still do not have it. p>

sequelize db:migrate

When the process is finished, your column must already be migrated. As a final recommendation, consult the documentation .

answered by 13.07.2017 / 18:26

The problem you pose (a data model that evolves) is a common thing. The solution I use in these cases is to create migrations .


answered by 12.07.2017 в 04:44