Sequelize migration with enum values
1 min read

Sequelize migration with enum values

I use Postgres, NodeJS, and Sequelize for my webapp.
I was having a hard time changing a column to add an enum value.
It turns out that Sequelize and Postgres doesn't allow updating enum values easily.
I had to run a manual query in my sequelize migration script.

'use strict'

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.sequelize.query(`ALTER TYPE "enum_type_name" ADD VALUE 'new_value'`);
  },

  down: (queryInterface, Sequelize) => {
    return queryInterface.sequelize.query(`
        DELETE 
        FROM
            pg_enum
        WHERE
            enumlabel = 'new_value' AND
            enumtypid = (
                SELECT
                    oid
                FROM
                    pg_type
                WHERE
                    typname = 'enum_type_name'
            )
    `);
  }
}

Replace enum_type_name and new_value to your own variables