Search code examples
phplaravellaravel-5artisan-migratemysql-error-1068

Laravel migration shows "Multiple primary key defined"


My Laravel migration is like below

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();
    });
}

When I ran php artisan migrate, it shows the following error:

1068 Multiple primary key

Could someone help to find the problem?


Solution

  • In Laravel Eloquent ORM the type increments will be defined as primary key automatically. So don't need to use primary() method.

    If the column is integer.

    $table->increments('user_sn');
    

    If the column is string

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

    If you want any other column to be unique (instead of primary key)

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