Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Implement AUTO_INCREMENT in a Yii2 Migration Using MySQL?

Tags:

php

mysql

yii2

I have created a yii2 (v2.0.6) migration for a simple MySQL (v5.6.21) table. Everything works, except that I cannot figure out how to AUTO_INCREMENT the primary key. The problem seems to be that I am using a small integer rather than the more standard long integer datatype. 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 get around the problem by using the ->primaryKey() method, which is commented out in line 3 above, but then yii creates a long integer datatype, and I am trying to avoid that. Any insight into the problem will be much appreciated.

like image 320
ZorbaTheGeek Avatar asked Dec 06 '15 11:12

ZorbaTheGeek


1 Answers

Why not a simply primaryKey?, the format for integer(8) , integer(11) or primary key is always the same is always an integer long then or you need a small int (max 5 digit) or you can use the normal $this->primaryKey() because

SMALLINT is for storage of 2 byte (value -32768 32767) an then smallInteger(8) is not coherent. the numer 8 is for output not for store format. If you want 8 digit you need at least INT of 4 byte -2147483648 2147483647 or more

like image 71
ScaisEdge Avatar answered Oct 25 '22 10:10

ScaisEdge