Tag Archives: migration

How to use PostGres Enum in Phinx

Currently, Phinx doesn’t support enum for postgres: https://github.com/cakephp/phinx/issues/891. Someone mentioned a solution on the issue (https://github.com/cakephp/phinx/issues/891#issuecomment-774496499).

PHP
$this->execute("create type foo as enum ('bar1', 'bar2')");
$this->execute('alter table table_name add column foo_column foo');

This works! In my project, I’ve used this pattern multiple times. E.g.

PHP
// table to track bag-sample relationship.
$bagSample = $this->table('bag_sample_relationv1');
$bagSample->addColumn('sample_bag_code', 'string', ['null' => false])
->addColumn("sample_code", "string", ['null' => false, 'limit' => 20])
->addColumn('added_at', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
->addIndex(['sample_bag_code', 'sample_code'], ['unique' => true])
->save();
// add a column with ENUM type.
$this->execute("create type BAG_SAMPLE_STATUS as ENUM('INSIDE', 'OUTSIDE', 'REMOVED')");
$this->execute("ALTER TABLE bag_sample_relationv1 ADD COLUMN status BAG_SAMPLE_STATUS DEFAULT 'INSIDE'");