14

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.

1

5 Answers 5

12

If it is critical to have that column type, you can always change it:

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

(I've tested this with MySQL - it works)

However, like @scaisEdge says, it's usually not worth the troble.

5

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

6
  • A downside to primaryKey() is the uncertainty which datatype you get. This is problematic once you want to create foreign key as the referencing column must be of the same type. Do you have any suggestions how to circumvent this problem?
    – DBX12
    Sep 29, 2019 at 14:28
  • @DBX12 your comment is not clear to . me .. in Yii2 the default primary key data type is integer .. but you can redefine as your prefer the column and the data type
    – ScaisEdge
    Sep 29, 2019 at 15:35
  • I mean, am I guaranteed to get always get the same type as I would get from integer()? Or is it depending of the database software (32-bit vs 64-bit machine, versions)? It would be bad if a primaryKey() internally maps to BIGINT for a specific database software and I use integer() (which maps to an INT) for all referencing columns as it worked for me on my test systems. Do you get what I mean?
    – DBX12
    Sep 29, 2019 at 16:27
  • 1
    @DBX12 You could check better in yii2 pk data type reference docs .. but for what i remember the pk data type is integer(11) .. and is used as default for prumaryKey() function in table creation ... hope is useful ..
    – ScaisEdge
    Sep 29, 2019 at 17:09
  • Thank you for the clarification, I hope the docs shed some light on the logic the type for the PK is selected.
    – DBX12
    Sep 29, 2019 at 17:15
2
    $this->createTable('posts', [

        'post_id' => "bigint(20) unsigned NOT NULL AUTO_INCREMENT",

        'loc_id' => $this->integer(10)->unsigned()->notNull(),

        "PRIMARY KEY (`post_id`,`loc_id`)",

    ], 'ENGINE=InnoDB DEFAULT CHARSET=utf8');
1

This works for me

$this->createTable('new_table',[
        'id' => Schema::TYPE_PK.' NOT NULL AUTO_INCREMENT',
        'name' => Schema::TYPE_STRING,
        'age' => Schema::TYPE_INTEGER
    ]);

However you can simply use the below style and Yii will replace the 'pk' type of id based on your DBMS. for MYSQL it will be int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY

$this->createTable('new_table',[
        'id' => 'pk',
        'name' => Schema::TYPE_STRING,
        'age' => Schema::TYPE_INTEGER
    ]);
0

Another (imo more readable) approach is:

$this
    ->integer()
    ->unsigned()
    ->notNull()
    ->append('AUTO_INCREMENT');
2
  • That throws error Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key in mysql
    – Radon8472
    Jul 1, 2022 at 14:17
  • 1
    ->append('AUTO_INCREMENT PRIMARY KEY') works fine in mysql
    – Radon8472
    Jul 1, 2022 at 14:24

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.