2

When I changed my primary key from increments to string (user_id) i now got an error in my migrations. I dont seem to see where I went wrong. my migrations files were okay until I changed from (->increments and the other one ->integer) to (->string on both). The problem is when it migrates clanmembers table, it says foreign key is incorrectly formed

My code before. Seem to be okay, nothing wrong with the migration in cmd

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('user_id');
        $table->string('fname', 50);
        $table->string('lname', 50);
        $table->integer('age');
        $table->string('gender', 10);
        $table->string('address', 50);
        $table->timestamps();
    });
}

public function up()
{
    Schema::create('clanmembers', function (Blueprint $table) {
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('user_id')->on('users')->onDelete('cascade');
        $table->integer('clan_id')->unsigned();
        $table->foreign('clan_id')->references('clan_id')->on('clans')->onDelete('cascade');
        $table->string('bandrole', 50);
        $table->timestamps();
    });
}

My code now after I changed ->increments and ->integer into both ->string

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->string('user_id');
        $table->string('fname', 50);
        $table->string('lname', 50);
        $table->integer('age');
        $table->string('gender', 10);
        $table->string('address', 50);
        $table->timestamps();
    });
}

public function up()
{
    Schema::create('clanmembers', function (Blueprint $table) {
        $table->string('user_id');
        $table->foreign('user_id')->references('user_id')->on('users')->onDelete('cascade');
        $table->integer('clan_id')->unsigned();
        $table->foreign('clan_id')->references('clan_id')->on('clans')->onDelete('cascade');
        $table->string('clanrole', 50);
        $table->timestamps();
    });
}
Ralph
  • 193
  • 1
  • 4
  • 12
  • Possible duplicate of [Laravel 5.2 - Use a String as a Custom Primary Key for Eloquent Table becomes 0](https://stackoverflow.com/questions/34582535/laravel-5-2-use-a-string-as-a-custom-primary-key-for-eloquent-table-becomes-0) – Joel Hinz Jul 30 '17 at 10:54

2 Answers2

2

Table One

 public function up()
    {
        //
       Schema::create('table_1', function (Blueprint $table) {
        $table->string('user_id',100);
        $table->string('fname', 50);
        $table->string('lname', 50);
        $table->integer('age');
        $table->string('gender', 10);
        $table->string('address', 50);
        $table->timestamps();
        $table->primary(['user_id']);
    });
   }

Table Two

public function up()
    {
        //
       Schema::create('clanmembers', function (Blueprint $table) {
        $table->string('user_id',100);
        $table->foreign('user_id')->references('user_id')->on('table_1')
        ->onDelete('cascade');
        $table->timestamps();
    });
   }
Masud Miah
  • 236
  • 1
  • 4
0

you need to set string length $table->string('user_id', 100); for intiger type if length is not set it automatically go for default value 11. But for the string, you need to set it.

Masud Miah
  • 236
  • 1
  • 4