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();
2
  • Are you using sequelize ? Commented Jul 29, 2020 at 5:43
  • @RohitAmbre Yes I am! Sorry for not specifying it. I'm taking over a project that was started by someone else and not familiar with dbs or migration in general! Commented Jul 29, 2020 at 5:45

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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?
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.
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.
what is set as dialect in Database config ?
Yes, that was the issue. Thanks a lot. Your initial answer fixed my problem as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.