Search code examples
databasedatabase-designormsequelize.jssequelize-cli

Making change in database model - Sequelize


I'm a beginner and I'm using Sequelize for my database. I created the model, did a migration after creating it, then added mocks & seeds but realized that I made a mistake for two attributes' data types. I need to change that but I'm not sure what the correct way to do this.

Do I have to:

sequelize db:migrate:undo

and then make my changes in the model and then run a migration?

Or do I have to drop the model and recreate it?

I guess, I'm not completely sure what a migration does in the scheme of things.


Solution

  • A migration will generate the sql queries and create the database tables.

    A model tells sequelize about the table that the above migration created. For example, when you do User.create(), sequelize needs to know the table details to translate that into sql query.

    I feel that as a beginner, you are probably better off creating the migrations yourself, rather than using the model to generate that.

    Coming to your question , you can safely use sequelize db:migrate:undo to delete the tables.
    As far as I know, sequelize db:migrate:undo method is not related to your model , so it does not matter if you edit it before or after the migration.

    To be on the safe side, you can delete the tables first using migrate:undo and then edit the models and run the migrations again.