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.