tsanchev tsanchev - 2 months ago 21
MySQL Question

Yii2 - Create Migration with LONGBLOB field

I want to create a table with a LONGBLOB field in it.

'image' => $this->binary(),


produces BLOB field in the table.

Is there any other way to produce LONGBLOB field except using raw SQL syntax for the specific field.

Below is my full code for creating the table.

$this->createTable('edition_images', [
'image_id' => $this->bigPrimaryKey()->unsigned(),

'embed_url' => $this->string()->notNull(),

'image_type' => $this->string(),
'image_md5' => $this->string(),
//'image' => $this->binary(),
'`image` longblob NULL',

'title_en' => $this->string(),
'title_bg' => $this->string(),
'title_ro' => $this->string(),

'order' => $this->bigInteger(20)->unsigned()->null(),

'edition_id' => $this->bigInteger(20)->unsigned()->notNull(),

'created_by' => $this->bigInteger(20)->unsigned()->notNull(),
'created_at' => $this->timestamp()->notNull()->defaultExpression('CURRENT_TIMESTAMP'),
'updated_by' => $this->bigInteger(20)->unsigned()->null(),
'updated_at' => $this->timestamp()->null()->defaultValue(null)->append('ON UPDATE CURRENT_TIMESTAMP'),
'deleted_by' => $this->bigInteger(20)->unsigned()->null(),
'deleted_at' => $this->timestamp()->null(),
'deleted' => $this->integer(1),
]);

Answer

You can specify the length as a parameter in the binary method. Since the longblob is 4GB you have to specify this in bytes:

'image' => $this->binary(4294967295),

You can also pass the exact column type as text:

'image' => 'LONGBLOB',