Migration on ENUM with postgres dialect: type already exists
Deleting previously existing ENUM column and re-creating it but with a different ENUM results in:
Executing (default): ALTER TABLE "Trackers" DROP COLUMN "type";
Executing (default): CREATE TYPE "enum_Trackers_type" AS ENUM('image', 'javascript', 'snippet'); ALTER TABLE "Trackers" ADD COLUMN "type" "enum_Trackers_type";
Possibly unhandled SequelizeDatabaseError: error: type "enum_Trackers_type" already exists
That's a postgres issue. Dropping an enum column does not drop the enum itself (since an enum might be used in several tables as I understand it)
@janmeier: Any idea how to correctly edit enum values through migration then?
Using raw query meanwhile, unless someone comes up with a better suggestion.
@kytwb
Any idea how to correctly edit enum values through migration then?
I was able to modify an ENUM by:
- Modifying the array assigned to the
valuesproperty of the configuration object passed as the second argument tosequelize.defineto include a new value - Create a migration in which the
upsimply callsModelNameHere.sync()
That generated the SQL statements required to modify the ENUM-type to match what I'd expressed in JavaScript code.
Erin
@laser Tried what you said, it does work when you add only one new value at a time.
Executing (default): ALTER TYPE "enum_warehouses_type" ADD VALUE 'postmaster' BEFORE 'amazon'
Executing (default): ALTER TYPE "enum_warehouses_type" ADD VALUE 'amazon' AFTER 'postmaster'
Possibly unhandled SequelizeDatabaseError: "amazon" is not an existing enum label
With amazon and postmaster being the two new values here.
I am facing the same issue. I was able to bypass it with using raw query
module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.sequelize.query("CREATE TYPE \"enum_users_sellerAccountType\" AS ENUM(\'sa\', \'sm\'); ALTER TABLE \"users\" ADD COLUMN \"sellerAccountType\" \"enum_users_sellerAccountType\";");
},
down: function (queryInterface, Sequelize) {
return queryInterface.sequelize.query("DROP TYPE \"enum_users_sellerAccountType\"; ALTER TABLE \"users\" DROP COLUMN \"sellerAccountType\";");
}
};
Is it a bug in sequelize, that causes enum migration to fail?
Facing same issue. To add to the problem, sequelize only allows changing default value, allowance of null or the data type. If you want to change some other attribute of a column, you will have to find a hack for it.
Heads up that if you decide to do raw queries like @Victor08 points out above, the down migration should drop the column first so that nothing depends on the data type, like this:
down: function (queryInterface, Sequelize) {
return queryInterface.sequelize.query("ALTER TABLE \"users\" DROP COLUMN \"userType\"; DROP TYPE \"enum_users_userType\";");
}
We have QueryInterface.dropAllEnums(), I think we should add a simple dropEnum() method.
@felixfbecker is there an update on the dropEnum() method? This would really be very handy. What is the context of QueryInterface.dropAllEnums()? Is it the whole database or maybe a table?
No, there is no update. Can't answer your second question.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
This is still an issue
This is how I solved it, not the best way but it allows adding a new field with an existing enum without it throwing an error.
queryInterface.addColumn(
'Users',
'language', {
type: Sequelize.ENUM('nl', 'en'),
allowNull: false,
defaultValue: 'nl'
}).then(function (response) {
console.log(response);
done();
}).catch(function (err) {
return queryInterface.sequelize.query(`ALTER TABLE "Users" ADD COLUMN "language" "public"."enum_Users_language" NOT NULL DEFAULT 'nl';`)
.then(function (response) {
console.log(response);
done();
}).catch(function (err) {
done();
});
});
finally got it working:
'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.
Any plans for adding QueryInterface.dropEnum() method?
@Edudjr that almost worked for me, but I had to make an ever so slight change to your up function:
up: (queryInterface, Sequelize) => {
return queryInterface.sequelize.query(`ALTER TYPE "enum_type_name" ADD VALUE 'new_value'`);
}
As you can see, I had to wrap my enum name in double quotes; for whatever reason, that's how Sequelize created my enum, so without the double quotes I got a "type does not exist" error. I'm not sure if this is a standard for Sequelize, but I figured I'd post my findings here in case anyone else ran into a similar issue. Also note that due to the use of both single and double quotes, I wrapped the query in tick marks since I'm using a new enough version of Node that supports template strings, thereby avoiding the need to escape any strings.
And for the down function, I made similar changes for readability:
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'
)
`);
}
Strangely enough, enum_type_name here in the pg_type table did not have double quotes in its actual value, unlike the type that was created.
See my comment https://github.com/sequelize/sequelize/issues/7151#issuecomment-344246084.
I've written a function to entirely replace the ENUM. It works inside a transaction and supports default value.
Hope you find it helpul.
I asked this on SO here and this is something sweet and simple that seems to be doing it
down: (queryInterface, Sequelize) =>
queryInterface.removeColumn('TableName', 'ColumnName')
.then(() => queryInterface.sequelize.query('DROP TYPE "enum_TableName_ColumnName";'));
};
The best way to modify enum values is to queryInterface.changeColumn:
module.exports = {
up : function (queryInterface, Sequelize) {
return queryInterface
.changeColumn(tableName, 'status', {
type: Sequelize.ENUM('a', 'b', 'c', 'd'),
allowNull: false
});
},
down: function (queryInterface, Sequelize) {
return queryInterface
.changeColumn(tableName, 'status', {
type: Sequelize.ENUM('a', 'b', 'c'),
allowNull: false
});
}
};