A
A
Alexey2017-09-14 00:21:02
Yii
Alexey, 2017-09-14 00:21:02

How to create JSON field in migration for MySQL?

Hello!
Yii2
In the migration in the safeUp() method, I create a table for MySQL.

$this->createTable('mytable', [
            'id' => $this->primaryKey(11)->unsigned(),
            'field_1' => $this->string(32)->notNull()->unique(),
            'field_2' => $this->string(100)->notNull()->unique(),
            'status' => $this->smallInteger(3)->notNull()->unsigned()->defaultValue(1),
            'dt_create' => $this->dateTime()->notNull(),
            'dt_update' => $this->dateTime()->notNull(),
            'param' => 'JSON NOT NULL', // Тут нужно создать поле типа  JSON 
        ], $tableOptions);

The line
'param' => 'JSON NOT NULL',
looks like a crutch, but how to do it right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2017-09-14
@slo_nik

Goodnight.
There is no such field type in the documentation.
Dostring()->notNull();

R
Ramoder, 2017-09-14
@Ramoder

Look for third party extensions. For example, for postgre there is https://github.com/Tigrov/yii2-pgsql, which will allow you to work normally with json, I think there is also for mysql. But I agree with matperez - 'param' => 'JSON NOT NULL',this is the correct solution.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question