Mary
Mary

Reputation: 37

How to use new created custom Postgres schema type (ENUM) in Yii2

Using a Postgres with Yii2 created a new ENUM type in postgresql with

createCommand("CREATE TYPE colorEnum AS ENUM ('red', 'black', 'white'););

How can I use it then in Yii2 Migrate class when creating a table, like:

$this->createTable('myTable’, [
            'color' =>  'what should go here?'

Upvotes: 1

Views: 479

Answers (1)

Salem Ouerdani
Salem Ouerdani

Reputation: 7886

Any of the following should work, including what @rob006 did suggest as comment:

public function safeUp()
{
   $this->execute("CREATE TYPE colorEnum AS ENUM ('red', 'black', 'white')");

   $this->createTable('myTable', [
       "color0" => "colorEnum",
       "color1" => "colorEnum  default 'black'",
       "color2" => $this->getDb()->getSchema()->createColumnSchemaBuilder("colorEnum  default 'black'"),
   ];

   // ...
}

public function safeDown()
{
    // ...
    $this->execute('DROP TYPE colorEnum');
}

Upvotes: 2

Related Questions