繁体   English   中英

Laravel 迁移显示“定义了多个主键”

[英]Laravel migration show "Multiple primary key defined"

我的 Laravel 迁移如下所示

public function up()
{
    Schema::create('account_main', function (Blueprint $table) {
      $table->increments('user_sn')->primary();
      $table->string('member_username', 20);
      $table->string('login_password', 255);
      $table->integer('login_count')->default('0')->unsigned();
    });
}

当我运行“php artisan migrate”时,显示错误“1068 Multiple primary key”。 有人可以帮忙找出问题所在。

您不需要->primary()因为已经->increments('...')包含它。
就像在 MySQL 中你这样写:

PK INT AUTO_INCREMENT PRIMARY KEY;
PRIMARY KEY(PK)

您声明了两次相同的主键

Laravel Eloquent ORM ,类型increments将自动定义primary key 所以不需要使用primary()方法。

如果列是整数。

$table->increments('user_sn');

如果列是字符串

$table->string('user_sn')->primary();

如果您希望任何其他列是唯一的(而不是主键)

$table->increments('user_sn');
$table->string('member_username', 20)->unique(); // cannot contain duplicate values

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM