phinx
phinx copied to clipboard
ENUM column type on PostgreSQL adapter
Hi,
I'm tryin to migrate some of my projects to Phinx (its great!), but I cannot complete the migration because Phinx don't support ENUM column types on PostgreSQL adapter...
Are there any reason for that, or I'm doing something wrong?
Regards!!
any update here from the team?
Side note: Afaik only few DBs (like MySQL) have enum support. That is also why CakePHP does not have native support for it, as it is a snowflake kind of thing. I recommend using integer columns and some low level framework or project PHP abstraction to simulate enums. This is then also DB agostic.
Sure, but in Phinx there are a lot of things only applies for one DB adapter (signed
columns for some data types in MySQL, timezone
on timestamp data types in PostgreSQL, etc) so I think "DB agnostic" is not a good reason to don't support some interesting and usefull features (like enum
types, in my opinion)
Fair enough Are you able to provide a pull request with your suggested changes?
I'd love to do it, but I'm not sure if I can. I'll try it...
@bhean Did you manage to do this?
I'm not sure how one would integrate this into Phinx. To use enums in PostgreSQL, you have to do CREATE TYPE identifier AS ENUM ('value1', 'value2', 'value3', ...);
The example they give on their docs is:
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
CREATE TABLE person (
name text,
current_mood mood
);
I'm guessing it'd be something like when in createTable()
, loop through all columns and if any are enums, generate the CREATE TYPE
statement for it, issue that, and then continue to generating the create table statement, substituting out the 'enum' type for the name we used in the CREATE TYPE
(and in changeColumn
, if the new type is enum, then do the CREATE TYPE
business like above). Would you want an additional option name
so that people could name their enums instead of having Phinx auto-generate the name?
@MasterOdin Yes I need it
enums have tricky requirements.
Closing due to lack of activity or out of scope. If there is new interest, please open a PR.
@othercorey can this be re-opened. It's annoying to do, but something phinx should probably support at some point.
Is anyone working on this, because i just hit this today having to support enums in pgsql ?
This is a quick hack for anyone who want to add 'enum' field to a postgresql table.
Use execute
method to create enum type and then alter table:
$this->execute("create type foo as enum ('bar1', 'bar2')");
$this->execute('alter table table_name add column foo_column foo');