D
D
Dilik Pulatov2020-02-04 10:33:30
PostgreSQL
Dilik Pulatov, 2020-02-04 10:33:30

How to correctly add data type in migration(postgresql) Yii2?

Good afternoon
This is an example migration file

public function safeUp() {
  $this->execute('DROP TYPE IF EXISTS users_status;');
  $this->execute("CREATE TYPE user_status AS ENUM ('active','inactive','blocked','deleted');");

  $this->createTable('{{%user}}', [
    'id'           => $this->bigPrimaryKey(),
    'status'       => 'user_status',
    'phone'        => $this->bigInteger(),
    'email'        => $this->string(50),
    'first_name'   => $this->string(30),
    'last_name'    => $this->string(30),
    'middle_name'  => $this->string(30),
    'password'     => $this->string(100),
    'create_at'    => $this->bigInteger(),
    'update_at'    => $this->bigInteger()
  ]);

  $this->createIndex('idx-user-status', '{{%user}}', 'status');
  $this->createIndex('idx-user-phone', '{{%user}}', 'phone');
  $this->createIndex('idx-user-email', '{{%user}}', 'email');
}


how to add default value for status field ?
or is there an option to add a data type simpler than mine?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arman, 2020-02-04
@Arik

Maybe something like this:

'status' => new \yii\db\Expression('user_status default "active"'),

with such migrations, it would be good to check what type of database and if it is not postgresql then throw an exception.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question