外键格式不正确

Foreign key is incorrectly formed

当我将主键从增量更改为字符串 (user_id) 时,我的迁移出现错误。我似乎看不出哪里出了问题。我的迁移文件没问题,直到我从 (->increments 和另一个 ->integer) 更改为 (->string on both)。问题是当它迁移氏族成员时table,它说外键格式不正确

我之前的代码。好像没问题,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();
    });
}

我现在的代码在我将 ->increments 和 ->integer 更改为 ->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();
    });
}

您需要设置字符串长度 $table->字符串('user_id', 100); 对于整数类型,如果未设置长度,它会自动使用默认值 11。 但是对于字符串,你需要设置它。

Table一个

 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两个

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