How to add or delete columns in Sequelize CLI?

Sometimes, we want to add or delete columns in Sequelize CLI.

In this article, we’ll look at how to add or delete columns in Sequelize CLI.

How to add or delete columns in Sequelize CLI?

To add or delete columns in Sequelize CLI, we can use the sequelize migration:create command to create a migration file.

Then we call addColumn to add a column and removeColumn to remove a column in the migration file.

For instance, we we run

sequelize migration:create --name name_of_your_migration

to create a migration file.

Then we write

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

  },

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

in the migration file.

We call queryInterface.addColumn with the table name, column name, and the column type to add a new column.

And we call queryInterface.removeColumn with the table name and column name to remove a column.

Conclusion

To add or delete columns in Sequelize CLI, we can use the sequelize migration:create command to create a migration file.

Then we call addColumn to add a column and removeColumn to remove a column in the migration file.