Laravel 迁移失败多个主键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34880277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:03:48  来源:igfitidea点击:

Laravel migration fails multiple primary keys

laravelmigrationprimary-key

提问by Maantje

I am trying to create a Migration in Laravel but it fails saying I got multiple primary keys.

我正在尝试在 Laravel 中创建一个迁移,但它没有说我有多个主键。

public function up()
{
    Schema::create('spins', function (Blueprint $table) {
        $table->integer('rid', true, true);
        $table->bigInteger('pid');
        $table->integer('result');
        $table->integer('bet');
        $table->timestamps();
        $table->primary(array('rid', 'pid'));
    });
}

The error:

错误:

SQLSTATE[42000]: Syntax error or access violation: 1068 Multipleprimary key defined 
(SQL: alter table `spins` add primary key `spins_rid_pid_primary` (`rid`, `pid`))      

回答by P??

The autoincrement of ridis the problem (second parameter in the line below).

的自动增量rid是问题(下一行中的第二个参数)。

$table->integer('rid', true, true);

If you are using InnoDB as MySQL engine it doesn't allow composite primary keys with an auto increment.

如果您使用 InnoDB 作为 MySQL 引擎,它不允许具有自动增量的复合主键。

Butif you change to the MyISAM engine it would be possible to do so.

但是,如果您更改为 MyISAM 引擎,则可以这样做。

  1. Add $table->engine = 'MyISAM';to your Migration.

  2. Declare the ridfield as a normal integer column

  3. Laravel doesn't provide a method to change existing columns so you need to run a raw SQL query: DB::statement('ALTER TABLE spins MODIFY rid INTEGER NOT NULL AUTO_INCREMENT');

  1. 添加$table->engine = 'MyISAM';到您的迁移。

  2. 将该rid字段声明为普通整数列

  3. Laravel 不提供更改现有列的方法,因此您需要运行原始 SQL 查询: DB::statement('ALTER TABLE spins MODIFY rid INTEGER NOT NULL AUTO_INCREMENT');



public function up()
{
    Schema::create('spins', function (Blueprint $table) {
        $table->engine = 'MyISAM';
        $table->integer('rid')->unsigned();
        $table->bigInteger('pid');
        $table->integer('result');
        $table->integer('bet');
        $table->timestamps();
        $table->primary(array('rid', 'pid'));

        DB::statement('ALTER TABLE spins MODIFY rid INTEGER NOT NULL AUTO_INCREMENT');
    });
}

回答by user1669496

Your primary key makes no sense.

你的主键没有意义。

You are adding a composite primary key to an auto incrementing column and another column. The auto incrementing column will already always be unique so you should just have only that be your primary key.

您正在向自动递增列和另一列添加复合主键。自动递增列将始终是唯一的,因此您应该只将其作为主键。

If you need pidto be unique, set ridto your primary key and add a unique key on pid.

如果您需要pid唯一,请设置rid为主键并在 上添加唯一键pid

Schema::create('spins', function (Blueprint $table) {
    $table->increments('rid');
    $table->bigInteger('pid');
    $table->integer('result');
    $table->integer('bet');
    $table->timestamps();
    $table->unique('pid');
});

If for some reason you do need your primary key to include ridand pid, this seems to work for me.

如果由于某种原因你确实需要你的主键包含ridand pid,这似乎对我有用。

CREATE TABLE `spins` (
  `rid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `pid` BIGINT(20) NOT NULL,
  `result` INT(11) NOT NULL,
  `bet` INT(11) NOT NULL,
  `created_at` TIMESTAMP NOT NULL,
  `updated_at` TIMESTAMP NOT NULL,
  PRIMARY KEY (`rid`, `pid`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

回答by Ben Harold

You can't have multiple primary keys on a single table. You can have a composite primary key, which is a primary key made from two or more columns. Apparently Blueprint does not support creating composite keys, so you'll have to use the query builder if you want to use composite keys.

一个表上不能有多个主键。您可以有一个复合主键,它是由两列或更多列组成的主键。显然Blueprint 不支持创建复合键,因此如果您想使用复合键,则必须使用查询构建器。

Otherwise you can just choose pidor ridas your primary key.

否则,您可以选择pidrid作为您的主键。