sequelize migration update column code example

Example 1: sequelize migration default value

queryInterface.addColumn('OrderBackups', 'my_column', {
  type: Sequelize.INTEGER,
  defaultValue: 0
})

Example 2: sequelize migration add column

module.exports = {
    up: (queryInterface, Sequelize) => {
        return queryInterface.sequelize.transaction((t) => {
            return Promise.all([
                queryInterface.addColumn('table_name', 'field_one_name', {
                    type: Sequelize.STRING
                }, { transaction: t }),
                queryInterface.addColumn('table_name', 'field_two_name', {
                    type: Sequelize.STRING,
                }, { transaction: t })
            ])
        })
    },

    down: (queryInterface, Sequelize) => {
        return queryInterface.sequelize.transaction((t) => {
            return Promise.all([
                queryInterface.removeColumn('table_name', 'field_one_name', { transaction: t }),
                queryInterface.removeColumn('table_name', 'field_two_name', { transaction: t })
            ])
        })
    }
};

Example 3: sequelize migration set default value

await queryInterface.addColumn('my_list', 'is_deleted', {
  type: Sequelize.BOOLEAN,
  defaultValue: false
});

Tags:

Misc Example