How to implement AUTO_INCREMENT in a Yii2 migration using MySQL?

I created a yii2 migration (v2.0.6) for a simple MySQL table (v5.6.21). Everything works, except that I cannot figure out how to make AUTO_INCREMENT the primary key. The problem is that I am using a small integer rather than the more standard long integer data type. Here is my migration code:

$this->createTable('{{%status}}', [
    'id' =>          $this->smallInteger(8)->unique(),
    //'id' =>        $this->primaryKey(11),
    'description' => $this->string(20),
]);

$this->addPrimaryKey('','status','id');

I could solve the problem with the → primaryKey () method, which is commented out on line 3 above, but then yii creates a long integer data type, and I try to avoid this. Any understanding of the problem would be greatly appreciated.

+4
source share
2 answers

, :

$this->createTable('{{%status}}', [
    'id'          => $this->primaryKey(11),
    'description' => $this->string(20),
]);
$this->alterColumn('{{%status}}', 'id', $this->smallInteger(8).' NOT NULL AUTO_INCREMENT');

( MySQL - )

, @scaisEdge, .

+3

primaryKey?, integer (8), integer (11) , int ( 5 ), $this->primaryKey(),

SMALLINT 2 ( -32768 32767), smallInteger(8) . 8 . 8 , INT 4 -2147483648 2147483647

+1

Source: https://habr.com/ru/post/1618777/


All Articles