19

I tried to run yii migrate, but it showed the following error:

create table news-cate ...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
The SQL being executed was: CREATE TABLE `news-cate` (
        `news-id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
        `cate-id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY

Here is my code:

class m150821_083020_create_newscate_table extends Migration
{
    public function safeUp()
    {
        $this->createTable('news-cate', [
            'news-id' => $this->primaryKey(),
            'cate-id' => $this->primaryKey(),
        ]);
        $this->addForeignKey("fk_news_cate_nid", "news-cate", "news-id", "news", "id", "RESTRICT", "CASCADE");
        $this->addForeignKey("fk_news_cate_cid", "news-cate", "cate-id", "category", "id", "RESTRICT", "CASCADE");
    }

    public function safeDown()
    {
        echo "m150821_083020_create_newscate_table cannot be reverted.\n";
        $this->dropTable("news-cate");
        return false;
    }
}

So how to create composite primary key by using Migration in Yii2?

arogachev
  • 33,150
  • 7
  • 114
  • 117
user1571234
  • 261
  • 3
  • 4
  • 9

3 Answers3

28

UPDATE

As of yii version 2.06 you can use new schema builder:

<?php
use yii\db\Migration;
...
$this->createTable('news-cate', [
    'news-id' => $this->integer()->notNull(),
    'cate-id' => $this->integer()->notNull(),
]);
$this->addPrimaryKey('news-cate_pk', 'news-cate', ['news-id', 'cate-id']);
...
?>

ORIGINAL ANSWER

Don't add primary keys in table creation, only declare types:

use yii\db\Schema;

,,,

$this->createTable('news-cate', [
    'news-id' => Schema::TYPE_INTEGER . ' NOT NULL',
    'cate-id' => Schema::TYPE_INTEGER . ' NOT NULL',
]);

After that you can add the composite primary key like this:

$this->addPrimaryKey('news-cate_pk', 'news-cate', ['news-id', 'cate-id']);

For multiple columns, array is allowed in addPrimaryKey() method.

This is better than writing raw sql.

Community
  • 1
  • 1
arogachev
  • 33,150
  • 7
  • 114
  • 117
9

try this way

public function safeUp()
{
   $this->createTable('news-cate', [
        'news-id' =>'int NOT NULL',
        'cate-id' =>'int NOT NULL',
        'PRIMARY KEY (news-id,cate-id)'
      ]);
    $this->addForeignKey("fk_news_cate_nid", "news-cate", "news-id", "news", "id", "RESTRICT", "CASCADE");
    $this->addForeignKey("fk_news_cate_cid", "news-cate", "cate-id", "category", "id", "RESTRICT", "CASCADE");
}
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
2

MOST EASIEST WAY

Ideally you're creating junction for two tables. You can use Yii command line to create migration by just one liner code :

php yii migrate/create create_junction_table_for_post_and_tag_tables --fields="created_at:bigInteger"

It will generates :

/**
 * Handles the creation for table `post_tag`.
 * Has foreign keys to the tables:
 *
 * - `post`
 * - `tag`
 */
class m160328_041642_create_junction_table_for_post_and_tag_tables extends Migration
{
    /**
     * @inheritdoc
     */
    public function up()
    {
        $this->createTable('post_tag', [
            'post_id' => $this->integer(),
            'tag_id' => $this->integer(),
            'created_at' => $this->dateTime(),
            'PRIMARY KEY(post_id, tag_id)',
        ]);

        // creates index for column `post_id`
        $this->createIndex(
            'idx-post_tag-post_id',
            'post_tag',
            'post_id'
        );

        // add foreign key for table `post`
        $this->addForeignKey(
            'fk-post_tag-post_id',
            'post_tag',
            'post_id',
            'post',
            'id',
            'CASCADE'
        );

        // creates index for column `tag_id`
        $this->createIndex(
            'idx-post_tag-tag_id',
            'post_tag',
            'tag_id'
        );

        // add foreign key for table `tag`
        $this->addForeignKey(
            'fk-post_tag-tag_id',
            'post_tag',
            'tag_id',
            'tag',
            'id',
            'CASCADE'
        );
    }

    /**
     * @inheritdoc
     */
    public function down()
    {
        // drops foreign key for table `post`
        $this->dropForeignKey(
            'fk-post_tag-post_id',
            'post_tag'
        );

        // drops index for column `post_id`
        $this->dropIndex(
            'idx-post_tag-post_id',
            'post_tag'
        );

        // drops foreign key for table `tag`
        $this->dropForeignKey(
            'fk-post_tag-tag_id',
            'post_tag'
        );

        // drops index for column `tag_id`
        $this->dropIndex(
            'idx-post_tag-tag_id',
            'post_tag'
        );

        $this->dropTable('post_tag');
    }
}

And Table structure would be like that :

+------------+------------+------+-----+---------+-------+
| Field      | Type       | Null | Key | Default | Extra |
+------------+------------+------+-----+---------+-------+
| post_id    | int(11)    | NO   | PRI | NULL    |       |
| tag_id     | int(11)    | NO   | PRI | NULL    |       |
| created_at | bigint(20) | YES  |     | NULL    |       |
+------------+------------+------+-----+---------+-------+

Please check below mentioned URL for reference:

http://www.yiiframework.com/doc-2.0/guide-db-migrations.html

Ravindra Bhalothia
  • 1,720
  • 2
  • 13
  • 16