9

I have a table called characters. Each character has an enum called "state", which can be "sad" or "happy". How do I add a new state "confused" to my column? I tried this migration but it failed:

migration.describeTable('characters').success(function (attributes) {
    migration.changeColumn('characters', 'state',
        {
            type: DataTypes.ENUM,
            values: ['sad', 'happy', 'confused']
        })
        .complete(done);
});

It complained that error: type "enum_characters_state" already exists.

5 Answers 5

17

Expanding on briangonzalez answer, you will also need a down function to undo the migration:

'use strict';

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

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

change enum_type_name and new_value to fit your needs.

Sign up to request clarification or add additional context in comments.

2 Comments

That's the most accurate and complete answer, this should be the one accepted for this question.
When searching for the enum inside pg_enum, are you in the context of one schema or all schemas?
9

With the latest version of Sequelize, you could achieve this by running a query (Postgres assumed):

module.exports = {
  up: function (queryInterface, Sequelize) {
    return queryInterface.sequelize.query("ALTER TYPE enum_character_state ADD VALUE 'confused';");
  }
};

Comments

5

I hope I am not late to the party..

this works for me

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

  down: async(queryInterface, Sequelize) => {
    await queryInterface.sequelize.query(`
      ALTER TYPE "enum_type_name" DROP VALUE 'confused';
    `);
  }
};

Comments

4

create a migration with following script:

module.exports = {
  up  : function (queryInterface) {
    return queryInterface
      .changeColumn(tableName, columnName, {
        values: arrayOfNewValues
      });
  },
  down: function (queryInterface) {
    return queryInterface
      .changeColumn(tableName, columnName, {
        values: arrayOfOldValues
      });
  }
};

2 Comments

This should be the accepted one. It's the less database-dependant solution.
This one doesn't work for me with PostgreSQL - it complains that the enum type already exists.
0

You need to remove the type, if you want to re-add it. Useful in down migrations if nothing else.

queryInterface.sequelize.query('DROP TYPE enum_characters_state;')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.