MySQL – Add a column before or after an existing column using Sequelize migrations

Sequelize provides addColumn method to add column to an existing table. But this will add the column as the last column of your table. While this may not affect the working of your database, you might want to add a column after a particular existing column.

In case of mysql, you can provide before or after in the options object being passed to the addColumn. For example, the following code in a migration will add a column called new_col after my_existing_col in the table called awesome.

queryInterface.addColumn(
'awesome',
'new_col',
{
type: Sequelize.STRING,
allowNull: false,
after: "my_existing_col"
}
)

PS: Make sure you check this with other dialects before changing your database type. This may or may not work with other databases depending upon support for before or after options.


2 Comments

So, what do you think ?