Guest-01
Guest-01

Reputation: 833

proper way to define or modify your model in sequelize with migrations

I'm a python main. I started node express project and I'm confused with commands that sequelize use for migrations.

In python flask-migrate, you write your model then doing flask db migrate makes a migration according to your model you've written. Then to apply your model to DB, you type flask db upgrade command.

But I've found that in Node.js, sequelize-cli commands are meant different. sequelize db:migrate actually "apply" existing migration which corresponds to upgrade command in python flask.

My questions is, then what is correct procedure to define or modify your model in Node.js? You have to make your model class "AND" migration script yourself then run db:migrate command?

It seems weird that I have to make same changes twice (Model & migration script) would there be another simple way?

Upvotes: 0

Views: 1000

Answers (1)

boc4life
boc4life

Reputation: 961

You can avoid doing this "double work" if you bypass migrations entirely and use the sequelize.sync() method, which builds the tables from your models.

The downside of this is that when you alter your models, you will need to pass in the { force: true } flag into the .sync() method in order for the new/altered attributes to be included. This option drops your tables and rebuilds them. This is fine for development, but awkward for production.

Upvotes: 1

Related Questions