phplaravellaravel-5laravel-migrationsmysql-error-1068

Laravel 5.6: Multiple primary key defined error


I created this migration script, but it gives the following error. It wont give any error if I changed the data type of the another column to string or something else. How to solve this issue?

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

SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined (SQL: alter table incidents add primary key incidents_id_primary(id))

SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined

Edit

Because nothing is working I moved this user_id creation to another migration script. Now it still fails to run the second migration script. Gives this error:

Syntax error or access violation: 1068 Multiple primary key defined (SQL: alter table incidents add user_id int unsigned not null auto_increment primary key)

It seems, if the primary key is not integer, then Laravel tries to make next first integer column primary!!!

So my first script is,

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

Second script is,

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

Solution

  • UPDATED

    Make it like this:

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

    And dont forget to add on your model

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

    Problem

    Acording to laravel docs

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

    The function you use receives a bool as parameter and the number 255 was being interpreted as true. I think the DB makes the autoIncrement a primary_key