Categories
JavaScript Answers

How to Add, Delete new Columns in Node Sequelize CLI?

Spread the love

To Add, Delete new Columns in Node Sequelize CLI, we create a migration file.

For instance, we run

$ sequelize migration:create --name name_of_your_migration

to create a migration.

Then we write

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.addColumn("Todo", "completed", Sequelize.BOOLEAN);
  },

  down: (queryInterface, Sequelize) => {
    return queryInterface.removeColumn("Todo", "completed");
  },
};

to call addColumn to add a column with the table name, column name, and data type.

We call removeColumn with the table name and column name to remove it.

Then we run

$ sequelize db:migrate

to call the up function in the migration file.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.