【问题标题】:Laravel migration show "Multiple primary key defined"Laravel 迁移显示“定义了多个主键”
【发布时间】:2020-05-10 09:14:47
【问题描述】:

我的 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”。 谁能帮忙找出问题。

【问题讨论】:

    标签: php laravel-5 artisan-migrate


    【解决方案1】:

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

    PK INT AUTO_INCREMENT PRIMARY KEY;
    PRIMARY KEY(PK)
    

    你声明了两次相同的主键

    【讨论】:

      【解决方案2】:

      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
      

      【讨论】:

        猜你喜欢
        • 2018-07-11
        • 2020-08-17
        • 2018-03-06
        • 1970-01-01
        • 2014-05-30
        • 2021-12-31
        • 2011-09-18
        • 2019-07-07
        • 1970-01-01
        相关资源
        最近更新 更多