New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migration on ENUM with postgres dialect: type already exists #2554
Comments
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. |
I was able to modify an ENUM by:
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.
With |
I am facing the same issue. I was able to bypass it with using raw query
Is it a bug in sequelize, that causes enum migration to fail? |
Facing same issue. To add to the problem, sequelize only allows changing |
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 |
@felixfbecker is there an update on the |
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.
|
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 |
@Edudjr that almost worked for me, but I had to make an ever so slight change to your 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: (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, |
See my comment #7151 (comment). 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
|
The best way to modify enum values is to
|
@tomprogers does it work with Postgres? |
@naartjie @tomprogers No, it does not seem to work on Postgres. |
@naartjie @tomprogers @jraut is not possible to "change" an enum in Postgres. You have to destroy it and create it again with the new values. Checkout this issue #7151 and this comment #7151 (comment). I created this npm package to change enums in Postgres: https://www.npmjs.com/package/sequelize-replace-enum-postgres. Hope it helps. Good luck! |
@abelosorio It seems like you've been able to alter ENUMs since at least Postgres 9.1https://www.postgresql.org/docs/9.1/static/sql-altertype.html so I'm not sure why you are saying it isn't possible. Can we get this issue reopened @sushantdhiman ? |
is not fixed |
This may be a bit off topic to this particular thread, but I wanted to post my solution to a similar problem incase anyone ends up here like I did. I was trying to set an ENUM along with
^^ This code threw the error
I am not very well versed in Sequelize (or in RDMS in general) but this bug kicked my butt. Hopefully if this is helpful to someone. And if this is bad practice, feel free to downvote it and tell me why. |
still not fixed 😭 |
I'm still running on the same problem |
This issue should definitely be reopened |
Hello, this thread is very long and was closed twice already... Can someone please provide an up-to-date MVCE for this? Something that I can just copy+paste and run and see the problem. If I can easily reproduce it, I will reopen this issue. Thanks!! |
i have this problem with code:
error: |
This comment was marked as spam.
This comment was marked as spam.
this works thanks for sharing. |
Deleting previously existing ENUM column and re-creating it but with a different ENUM results in:
The text was updated successfully, but these errors were encountered: