You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Migration on ENUM with postgres dialect: type already exists #2554
dialect: postgresFor issues and PRs. Things that involve PostgreSQL (and do not involve all dialects).type: bugDEPRECATED: replace with the "bug" issue type
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 values property of the configuration object passed as the second argument to sequelize.define to include a new value
Create a migration in which the up simply calls ModelNameHere.sync()
That generated the SQL statements required to modify the ENUM-type to match what I'd expressed in JavaScript code.
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?
Reacted by Sony Mathew, Souradeep Nanda, Aakash Verma, Ihor Sakayliuk, Eduardo J. López Morín, Nate, Patrick Sturm, Kaung Myat Lwin, Puskar , jeyalakshmiXfinite and 2 more
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){returnqueryInterface.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.
Reacted by Alex Mueller, Dmitriy Simushev, Jared Tyler, Souradeep Nanda, Sean Catlin, Jason Bodily, Abhilasha Bhatia, RyccoSN, Yulia Tenincheva, Jason Howmans and 15 more
@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?
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 🙂
Reacted by heekyu, Brent Kersanske, Daniel J Raine, Richard Scarrott, davidrac, Tom Keidar, Artsem Stalavitski, Maxime, Sérgio Deusdedith de Araujo Neto, Greg and 2 more
^^ This code threw the error enum_payments_entity_type already exists. Even when trying to manually drop it, alter the column, etc, I could never get it to work.
My solution was to set the column as a STRING first, then come back and set it as an ENUM.
await queryInterface.addColumn(
'payments',
'entity_type',
{
type: Sequelize.STRING, // NOTE THIS IS A STRING
allowNull: true // set to true to allow bulk update
},
{ transaction }
);
await queryInterface.bulkUpdate(
'payments',
{
entity_id: 1,
entity_type: SHIPPER
},
{},
{ transaction }
);
await queryInterface.changeColumn(
'payments',
'entity_type',
{
type: Sequelize.ENUM,
allowNull: false,
values: [SHIPPER, CARRIER]
},
{ transaction }
);
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.
Reacted by Scott Humphries, Shem, Adebanjo oluwatominiyin and jscul
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!!
Reacted by Michał Wilski, yardenmol, Greg and jscul
Any idea how to correctly edit enum values through migration then?
I was able to modify an ENUM by:
1. Modifying the array assigned to the `values` property of the configuration object passed as the second argument to `sequelize.define` to include a new value
2. Create a migration in which the `up` simply calls `ModelNameHere.sync()`
That generated the SQL statements required to modify the ENUM-type to match what I'd expressed in JavaScript code.
dialect: postgresFor issues and PRs. Things that involve PostgreSQL (and do not involve all dialects).type: bugDEPRECATED: replace with the "bug" issue type
Activity
janmeier commentedon Nov 10, 2014
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)
laser commentedon Jan 9, 2015
@kytwb
I was able to modify an ENUM by:
valuesproperty of the configuration object passed as the second argument tosequelize.defineto include a new valueupsimply callsModelNameHere.sync()That generated the SQL statements required to modify the ENUM-type to match what I'd expressed in JavaScript code.
Erin
Victor08 commentedon Feb 6, 2016
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?
delveintechnolabs commentedon Mar 12, 2016
Facing same issue. To add to the problem, sequelize only allows changing
default value,allowance of nullor thedata type. If you want to change some other attribute of a column, you will have to find a hack for it.thealmightygrant commentedon Oct 4, 2016
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:
felixfbecker commentedon Oct 5, 2016
We have
QueryInterface.dropAllEnums(), I think we should add a simpledropEnum()method.kgalli commentedon Feb 21, 2017
@felixfbecker is there an update on the
dropEnum()method? This would really be very handy. What is the context ofQueryInterface.dropAllEnums()? Is it the whole database or maybe a table?felixfbecker commentedon Feb 21, 2017
No, there is no update. Can't answer your second question.
stale commentedon Jun 29, 2017
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 🙂
13 remaining items
abelosorio commentedon Jun 18, 2018
@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!
robcresswell commentedon Aug 20, 2018
@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 ?
rostismal commentedon Dec 17, 2018
is not fixed
DaddyWarbucks commentedon Apr 10, 2019
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
allowNull: true, then do a bulk update, and thenchangeColumntoallowNull: false^^ This code threw the error
enum_payments_entity_type already exists. Even when trying to manually drop it, alter the column, etc, I could never get it to work.My solution was to set the column as a STRING first, then come back and set it as an ENUM.
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.
Dakkers commentedon Jul 16, 2019
still not fixed 😭
Otavioensa commentedon Jul 23, 2019
I'm still running on the same problem
Otavioensa commentedon Jul 23, 2019
This issue should definitely be reopened
papb commentedon Jul 23, 2019
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!!
N1ades commentedon Dec 14, 2019
i have this problem with code:
error:
(node:24616) UnhandledPromiseRejectionWarning: SequelizeDatabaseError: тип "enum_tags_type" уже существуетpapb commentedon Jan 16, 2020
I think issues #2577 and #7649 cover everything mentioned in this issue already.
navsaini53 commentedon Mar 3, 2022
RasenGUY commentedon Sep 11, 2022
this works thanks for sharing.