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.
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.
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';");
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With