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

ddrv8njm  于 11个月前  发布在  PHP
关注(0)|答案(1)|浏览(94)

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

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

字符串
SQL [42000]:返回错误或访问冲突:1068定义了多个主键(SQL:alter table incidents add primary key incidents_id_primaryid))
SQLSTATE[42000]:返回错误或访问冲突:1068定义了多个主键

编辑

由于没有任何工作,我将此user_id创建移动到另一个迁移脚本。现在它仍然无法运行第二个迁移脚本。给出此错误:
1068定义了多个主键(SQL:alter table incidents add user_id int unsigned not null auto_increment primary key)
看起来,如果主键不是整数,那么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);
   });
}

des4xlb0

des4xlb01#

已关闭

就像这样:

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)


您使用的函数接收bool作为参数,数字255被解释为true。

相关问题