Insolita / yii2-migrik

Yii2 Gii-tools for create migration files
106 stars 29 forks source link

Error when generating composite primary key #35

Closed krukru closed 7 years ago

krukru commented 7 years ago

I tried generating a migration for a table which has a composite (two-column) primary key. The result that I got from the generator was

    public function safeUp()
    {
        $tableOptions = 'ENGINE=InnoDB';
             $this->createTable('{{%test}}',[
               'a'=> $this->primaryKey(20),
               'b'=> $this->primaryKey(20),
               'c'=> $this->integer(11),
            ], $tableOptions);
    }

However this will not import on MariaDB 10.1.22 (and possible on MySQL) with the following error: Exception: SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key

I think that when there is a composite primary key, you should generate the PK using this example

    public function safeUp()
    {
        $tableOptions = 'ENGINE=InnoDB';
             $this->createTable('{{%test}}',[
               'a'=> $this->integer(11),
               'b'=> $this->integer(11),
               'c'=> $this->integer(11),
            ], $tableOptions);
        $this->addPrimaryKey('pk', '{{%test}}', ['a', 'b'])
    }