Laravel 5.6:多个主键定义的错误

时间:2019-02-13 16:27:10

标签: php laravel

我创建了此迁移脚本,但是出现以下错误。如果将另一列的数据类型更改为字符串或其他内容,它不会给出任何错误。如何解决这个问题?

    Schema::create('incidents', function (Blueprint $table) {
        $table->string('id');
        $table->primary('id');
        $table->unsignedInteger('user_id', 255);
    });

SQLSTATE [42000]:语法错误或访问冲突:1068已定义多个主键(SQL:alter table incidents添加主键incidents_id_primaryid

SQLSTATE [42000]:语法错误或访问冲突:1068定义了多个主键

修改

因为什么都没用,所以我将这个user_id创建移动到了另一个迁移脚本。现在它仍然无法运行第二个迁移脚本。给出此错误。

语法错误或访问冲突:1068定义了多个主键(SQL:alter table incidents添加user_id int unsigned not null auto_increment主键)

似乎,如果主键不是整数,那么laravel试图将下一个第一个整数列设为主!!

所以我的第一个脚本是

 public function up()
 {
     Schema::create('incidents', function (Blueprint $table) {
         $table->string('id')->primary();
     });
 }

第二个脚本是

public function up()
{
   Schema::table('incidents', function($table) {
     $table->unsignedInteger('user_id', 255);
   });
}

1 个答案:

答案 0 :(得分:1)

已更新

使它像这样:

Schema::create('incidents', function (Blueprint $table) {
     $table->string('id')->primary();
     $table->unsignedInteger('another_column');
});

别忘了添加模型

protected $primaryKey = 'id';
public $incrementing = false; //If you dont want the auto-increment

问题

根据laravel docs

unsignedInteger(string $column, bool $autoIncrement = false)

您使用的函数将布尔值作为参数,并且数字255被解释为true。我认为数据库将autoIncrement设为primary_key

相关问题