sequelize migration code example

Example 1: sequelize generate migration

npx sequelize-cli migration:generate --name add-title-post-table

Example 2: squelize-cli create empty migration

npx sequelize-cli migration:create --name your_migration_name

Example 3: sequelize migration default value

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

Example 4: 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 5: sequelize migration limit

queryInterface.createTable(
    'Posts',
    {
        title: {
            type: Sequelize.DataTypes.STRING(100),
            allowNull: false
        }
    }
);

Example 6: sequelize migarte

npx sequelize-cli db:migrate

Tags:

Php Example