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
feature: Add to changeColumn the ability to add new ENUM values #7151
I've had to declare the entire ENUM in the migration (up/down). Didn't think about what you're suggesting coz I guess that's how you'd handle it in plain sql.
Just curious as to what you have in mind ??
Reacted by Peter Mikitsh, Caio Flores, Raiss, Martin Pultz, Roman Paradeev, Aaron Ryden, Shefali Agarwal, Jan Burda, codewithtkssharma, Birhanu Hailemariam and 56 more
Reacted by Otávio Augusto, Luísa Braga, Lucas Gouvea, Rohan R, Alan Tirado, Charlie Kelly, Aabhas Karmacharya and sagiTip
Reacted by Vikash Choudhary, Atif Riaz, Eugene C., Sina Maleki, James M. Greene, Álvaro Souza, Jeric Guillermo, Jardel Matias, Faris Rayhan and Placide IRANDORA
I think it's fair to say that this should function identically to @Aukhan's code, but it does not. While the docs say you can only change "default value, allowance of null or the data type", that's all we are doing here, however we have to use the Sequelize interface in a specific way to get the desired output.
Reacted by Asad Khan, colkito, MaykonMorais, Patrick Hurley, Carlos Macario and João Canabarro
This does not work for me on PostgreSQL 9.6 with Sequelize [Node: 6.10.3, CLI: 2.7.0, ORM: 3.30.4].
I get the following error:
SequelizeDatabaseError: type "enum_processes_status" already exists
With the generated sql:
'ALTER TABLE "processes" ALTER COLUMN "status" SET NOT NULL;ALTER TABLE "processes" ALTER COLUMN "status" SET DEFAULT \'proposed\';CREATE TYPE "public"."enum_processes_status" AS ENUM(\'proposed\', \'on going\', \'completed\', \'rejected\');ALTER TABLE "processes" ALTER COLUMN "status" TYPE "public"."enum_processes_status" USING ("status"::"public.enum_processes_status");' }
It looks like it is creating the type, instead of altering it when it already exists.
Reacted by Sergey Podatelev, Ivan Montiel, Will Jehring, Max, Thomas Groutars, jraut, Aleksei Korkoza, Yahor, Outsider, louis.julien and 37 more
Hey @jakoandersen
Unfortunately I'm not so familiar with Postgres, really wish I was though.
Did you try @petermikitsh solution ?
I'm thinking maybe the ENUM() function call is triggering the create query in postgres. Somehow it should have IF NOT EXISTS in it.
@jakoandersen This is what my postgres migration ended up looking like to map an enum.
I needed to map some values so didn't try using changeColumn.
up(queryInterface, Sequelize) {
return queryInterface.sequelize.transaction(async (transaction) => {
// rename the enum
await queryInterface.sequelize.query(
`ALTER TYPE enum_my_table_enumcol RENAME TO enum_my_table_enumcol_old`,
{ transaction }
);
// create the new one
await queryInterface.sequelize.query(
`CREATE TYPE enum_my_table_enumcol AS ENUM ('enum1', 'enum2', 'newenum3', 'newenum4', 'enum5')`,
{ transaction }
);
// change the column to use the new one, mapping data
await queryInterface.sequelize.query(
`ALTER TABLE my_table
ALTER COLUMN enumcol DROP DEFAULT,
ALTER COLUMN enumcol TYPE enum_my_table_enumcol
USING (CASE
WHEN enumcol='oldenum3' THEN 'newenum3'::text
WHEN enumcol='oldenum4' THEN 'newenum4'::text
ELSE enumcol::text
END)::enum_my_table_enumcol,
ALTER COLUMN enumcol SET DEFAULT 'enum1'
`, { transaction });
// drop the old enum
await queryInterface.sequelize.query(
`DROP TYPE enum_my_table_enumcol_old`,
{ transaction }
);
});
},
Reacted by Max Techera, Abel Osorio, Ratikesh Misra, Paulo Afonso, Cuong, Yukai Huang and Ran Halprin
Reacted by loayg, Asad Khan, Timothée Astier, Diogo Filipe Costa, Thomas Groutars, Dylan Cutler, RyccoSN, Chi, Philippe Auriach, Karolina Behalova and 8 more
@robcresswell at the time of this issue I wasn't much literate with postegres but eventually found opportunity to start using it, the issue here is as @petermikitsh describes that you have to use the sequelize interface in a specific way to achieve it, indeed postgres supports it.
Activity
Aukhan commentedon Jan 26, 2017
I've had to declare the entire ENUM in the migration (up/down). Didn't think about what you're suggesting coz I guess that's how you'd handle it in plain sql.
Just curious as to what you have in mind ??
diosney commentedon Jan 26, 2017
This is how I'm doing it now, it is MySQL/MariaDB specific SQL code, so it might now work on Postgres, Oracle or SQlite:
The problem with that code is that is not adapter agnostic, that's why I want to use the
changeColumnmethod to do the alters, in a way like this one:which then will generate the specific SQL query for me.
diosney commentedon Jan 26, 2017
See http://sequelize.readthedocs.io/en/latest/docs/migrations/#skeleton
Aukhan commentedon Jan 26, 2017
Interesting ... this is how I attempted it and worked for me
diosney commentedon Jan 26, 2017
Nice! Worked! First time I use ENUMs like that :)
Thanks!
petermikitsh commentedon Feb 25, 2017
While @Aukhan's solution works, we are essentially saying the same thing if we write:
I think it's fair to say that this should function identically to @Aukhan's code, but it does not. While the docs say you can only change "default value, allowance of null or the data type", that's all we are doing here, however we have to use the Sequelize interface in a specific way to get the desired output.
jakoandersen commentedon Aug 3, 2017
This does not work for me on PostgreSQL 9.6 with Sequelize [Node: 6.10.3, CLI: 2.7.0, ORM: 3.30.4].
I get the following error:
With the generated sql:
'ALTER TABLE "processes" ALTER COLUMN "status" SET NOT NULL;ALTER TABLE "processes" ALTER COLUMN "status" SET DEFAULT \'proposed\';CREATE TYPE "public"."enum_processes_status" AS ENUM(\'proposed\', \'on going\', \'completed\', \'rejected\');ALTER TABLE "processes" ALTER COLUMN "status" TYPE "public"."enum_processes_status" USING ("status"::"public.enum_processes_status");' }It looks like it is creating the type, instead of altering it when it already exists.
Aukhan commentedon Aug 3, 2017
Hey @jakoandersen
Unfortunately I'm not so familiar with Postgres, really wish I was though.
Did you try @petermikitsh solution ?
I'm thinking maybe the
ENUM()function call is triggering thecreatequery in postgres. Somehow it should haveIF NOT EXISTSin it.marneborn commentedon Aug 5, 2017
@jakoandersen This is what my postgres migration ended up looking like to map an enum.
I needed to map some values so didn't try using
changeColumn.ablankenship10 commentedon Aug 28, 2017
Also getting this error with latest Postgres, Node and SequelizeCLI.
What exactly needs done to make @Aukhan 's format work as this is what I would expect the syntax to be.
loayg commentedon Nov 10, 2017
@ablankenship10 @jakoandersen @Aukhan @marneborn
For postgres, here is my workaround
You will obviously need to put the proper values for
tableName,colName, val1 ..etcabelosorio commentedon Nov 14, 2017
Function to use the @loayg workaround for PostgreSQL 👉 https://stackoverflow.com/questions/45437924/drop-and-create-enum-with-sequelize-correctly/47286023#47286023
Cheers
abelosorio commentedon Jan 10, 2018
I have used the above solution in three different projects, so I decided to write a npm module:
https://www.npmjs.com/package/replace-enum-postgresql.Use https://www.npmjs.com/package/sequelize-replace-enum-postgres instead.
Aukhan commentedon Jan 11, 2018
great work @abelosorio
just a thought, maybe you could name it like
sequelize-replace-enum-postgresabelosorio commentedon Jan 11, 2018
sure @Aukhan, I'll rename it.
flvingdutchman commentedon Apr 11, 2018
That's how you can alter Enum for a column with existing values in postgress. Values will stay.
(Unfortunately you can't run this queries inside a transaction block, see: https://www.postgresql.org/docs/9.1/static/sql-altertype.html)
robcresswell commentedon Aug 20, 2018
@Aukhan @abelosorio Am I missing something here? It seems like Postgres supports altering ENUMs just fine since at least 9.1: https://www.postgresql.org/docs/9.1/static/sql-altertype.html ?
Aukhan commentedon Aug 21, 2018
@robcresswell at the time of this issue I wasn't much literate with postegres but eventually found opportunity to start using it, the issue here is as @petermikitsh describes that you have to use the sequelize interface in a specific way to achieve it, indeed postgres supports it.
abelosorio commentedon Aug 21, 2018
Hi @robcresswell! You are exactly right. With
ALTER TYPEyou can add values to an ENUM, but you cannot remove values from it.Even in the last version (10: https://www.postgresql.org/docs/current/static/sql-altertype.html) removing values from enum is still unsupported.
Anyway, thanks for this. I'll adjust my package to use ALTER TYPE when the migration only requires to add new values.
lucasalexandre-cc commentedon Mar 4, 2021
Here is an example of up and down methods that works for me:
Thanks @abelosorio, based on your response I found http://www.michael1e.com/sequelize-enum-migration/ and he give me an example
Amitrochates commentedon Feb 4, 2026
how is this still not officially supported for postgres lol