0

I altered my models in node and on starting the application, the migration does not alter the postgresql table. I can see that the query CREATE TABLE IF NOT EXISTS is run on the table, but I guess it does not check whether a column has been added/removed. How can I do it through node so that such changes are automatically handled and migrated by my application? Currently I'm calling the sync() function to migrate.

let membres = await Membership.sync();
Rohit Ambre
  • 921
  • 1
  • 11
  • 25
Inception
  • 455
  • 1
  • 9
  • 32

1 Answers1

1

In sequelize you'll have to create migrations for every DB structure changes.

As you have adding new column then you need to create migration using sequelize-cli for alter your table. Now going forward whoever starts working on your project will just have to run those migrations and all DB changes would be handled by sequelize.

Check this post to get the idea of migrations. Or check sequelize docs here

Rohit Ambre
  • 921
  • 1
  • 11
  • 25
  • Thank you. Just for clarity, currently my migrate() function is simply doing the sync() operation on all tables. And migrate() is called on application start. Should I handle the table changes separately, a file in which only the modifications are added and then call it after the current migrate() function is run? – Inception Jul 29 '20 at 06:01
  • You'll have to do as mentioned in stackoverflow post I shared. it will create a separate file in which you'll write your columns and its type and then you'll run it with `migrate` command. – Rohit Ambre Jul 29 '20 at 06:05
  • I'm getting `ERROR: Please install mysql2 package manually` although I'm using Postgresql when I run the command `npx sequelize db:migrate`. I think it has something to do with not running in dev environment. – Inception Jul 29 '20 at 06:23
  • what is set as `dialect` in Database config ? – Rohit Ambre Jul 29 '20 at 06:51
  • Yes, that was the issue. Thanks a lot. Your initial answer fixed my problem as well. – Inception Jul 29 '20 at 07:11