How to make field enum migration yii2

CrashBurn picture CrashBurn · Aug 23, 2016 · Viewed 12.3k times · Source

I make field ENUM and the result is error when I use yii migrate/up on CMD windows.

public function up()
{
    $tableOptions = null;
    if ($this->db->driverName === 'mysql') {
        $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
    }

    $this->createTable('{{%user_social_media}}', [
        'social_media' => $this->ENUM('facebook', 'google', 'twitter', 'github'),
        'id' => $this->primaryKey(),
        'username' => $this->string(),
        'user_id' => $this->integer(11),
        'created_at' => $this->integer(11),
        'updated_at' => $this->integer(11),            
       ], $tableOptions);
}

When I migrate/up error

Answer

Bizley picture Bizley · Aug 23, 2016

There is no enum() method at the moment since not every DB is supporting ENUM fields. You can do it manually though:

'social_media' => "ENUM('facebook', 'google', 'twitter', 'github')",

Note: This solution is for Mysql only

For related Postgresql content visit here