Php SQLSTATE[HY000]:一般错误:1215无法添加外键约束错误

Php SQLSTATE[HY000]:一般错误:1215无法添加外键约束错误,php,laravel,foreign-keys,database-migration,Php,Laravel,Foreign Keys,Database Migration,我想在users表中添加role列。但我犯了这个错误 我的用户迁移 Schema::create('users', function (Blueprint $table) { $table->increments("id"); $table->string('name'); $table->string("company_name")->unique(); $tabl

我想在users表中添加role列。但我犯了这个错误

我的用户迁移

 Schema::create('users', function (Blueprint $table) {
        $table->increments("id");
        $table->string('name');
        $table->string("company_name")->unique();
        $table->string('email')->unique();
        $table->string("phone")->unique();
        $table->boolean("status");
        $table->integer("role")->unsigned();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();

        $table->foreign('role')->references('id')->on('roles')->cascadeOnUpdate()->cascadeOnDelete();

    });
Schema::create('roles', function (Blueprint $table) {
        $table->increments("id");
        $table->string('name');
});
以及我的角色迁移

 Schema::create('users', function (Blueprint $table) {
        $table->increments("id");
        $table->string('name');
        $table->string("company_name")->unique();
        $table->string('email')->unique();
        $table->string("phone")->unique();
        $table->boolean("status");
        $table->integer("role")->unsigned();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();

        $table->foreign('role')->references('id')->on('roles')->cascadeOnUpdate()->cascadeOnDelete();

    });
Schema::create('roles', function (Blueprint $table) {
        $table->increments("id");
        $table->string('name');
});
试着改变

$table->integer(“角色”)->unsigned();

$table->foreigned('role_id');
$table->foreign('role_id')->引用('id')->on('roles'))
->onUpdate('cascade')->onDelete('cascade');
在用户迁移之前,请确保角色迁移在迁移中具有预排序,
您可以编辑角色迁移的迁移名称(编辑时间戳以使其在用户之前迁移)

您需要按如下方式更改代码

  Schema::create('users', function (Blueprint $table) {
    $table->increments("id");
    $table->string('name');
    $table->string("company_name")->unique();
    $table->string('email')->unique();
    $table->string("phone")->unique();
    $table->boolean("status");
    $table->unsignedBigInteger("role");
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();

    $table->foreign('role')->references('id')->on('roles')->cascadeOnUpdate()->cascadeOnDelete();

});

根据文档,您使用了
integer
而不是
unsignedbiginger

问题很可能是由于错误的迁移顺序造成的。当您在
用户
表中存储
角色
id
时,您必须先迁移
角色
表,然后迁移
用户
表。由于我不知道您的表的顺序,我将根据您的帖子顺序,假设先迁移
用户
表,然后迁移
角色
表,这就是
用户
表无法获取
角色
表的id引用的原因。 尝试将
角色
表的日期重命名为
用户
表之前的日期,然后运行
php artisan缓存:清除
,然后
php artisan迁移:刷新

只是一些建议,正如您指定使用的是Laravel 8,请使用
id()函数,而不是使用
增量(“id”)。要遵循Laravel的外键规则,请使用
role\u id
作为外键,而不是
role

而不是使用:
$table->foreign('role')->references('id')->on('roles')->cascadeoupdate()->cascadeOnDelete()

使用:


$table->foreigned('role_id')->constrained()->onUpdate('cascade')->onDelete('cascade')

尝试更改
$table->integer(“角色”)->unsigned()
$table->biginger(“角色”)->unsigned()我希望您的
角色
迁移在
用户
迁移之前有一个时间戳,因为必须首先创建
角色
,顺序很重要。我尝试了bigInteger,但再次出现同样的错误。我想先尝试角色迁移迁移,但我现在不知道该怎么做?您必须将作为文件名前缀的时间戳调整为作为创建用户迁移前缀的时间戳之前的时间戳,或者将“创建用户”迁移中的代码块调整为在“角色”表中的代码块之后。i更改时间戳,但ı再次出错。您可以在用户迁移之前对代码部分i更改的角色迁移时间戳使用代码块。并按照您的建议更改代码。但ı再次出现该错误。我尝试了unsignedBigInteger,但再次出现相同的错误。很抱歉,如果在角色表中有以下内容,那么它是否有效,
$table->id()而不是
$table->增量(“id”)?我使用$table->增量(“id”);在角色表上。