1215一般错误:无法添加外键约束Laravel

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1215一般错误:无法添加外键约束Laravel相关的知识,希望对你有一定的参考价值。

我有一个具有已创建外键约束的表:

错误:

 SQLSTATE[HY000]: General error: 1215 Cannot add foreign key 
constraint (SQL: alter table `league_seasons` add 
constraint `league_seasons_league_id_foreign` foreign key (`league_id`)
 references `leagues` (`id`) on delete cascade)

这是联赛表

public function up() {
        Schema::create('leagues', function (Blueprint $table) {
            $table->integer('id');
            $table->increments('increment_id');
            $table->string('type')->nullable();
            $table->integer('legacy_id')->nullable();
            $table->integer('country_id')->nullable();
    }

这是League_seasons表

    public function up() {
        Schema::create('league_seasons', function (Blueprint $table) {
            $table->integer('id');
            $table->increments('increment_id');
            $table->string('name')->nullable();
            $table->unsignedInteger('league_id');
            $table->string('is_current_season')->nullable();
            $table->string('current_round_id')->nullable();
            $table->string('current_stage_id')->nullable();
            AppHelpersDbExtender::defaultParams($table, true);
        });

        Schema::table('league_seasons', function (Blueprint $table) {
            $table->foreign('league_id')->references('id')->on('leagues')->onDelete('cascade');

        });

    }

我已经尝试过交换unSignedInteger,BigInteger,但它们似乎都不起作用。知道为什么会这样吗?谢谢

答案

正如@repat指出的,在两个表上都添加带有index()的unSignedInteger对我来说很有效。

最终联赛表:

public function up() {
        Schema::create('leagues', function (Blueprint $table) {
            $table->unsignedInteger('id')->index();
            $table->increments('increment_id');
            $table->string('type')->nullable();
            $table->integer('legacy_id')->nullable();
            $table->integer('country_id')->nullable();
            $table->string('name')->nullable();
    }

最终联赛季节表:

    public function up() {
        Schema::create('league_seasons', function (Blueprint $table) {
            $table->integer('id');
            $table->increments('increment_id');
            $table->string('name')->nullable();
            $table->unsignedInteger('league_id')->index();
            $table->string('is_current_season')->nullable();
            $table->string('current_round_id')->nullable();
            $table->string('current_stage_id')->nullable();
            AppHelpersDbExtender::defaultParams($table, true);
        });

        Schema::table('league_seasons', function (Blueprint $table) {
            $table->foreign('league_id')->references('id')->on('leagues')->onDelete('cascade');

        });

    }

另一答案

要创建Foreign keychild column的数据类型必须与parent column完全匹配。例如,由于leagues.idINT,所以league_seasons.league_id也需要是INT,而不是unsignedInteger

因此改变

$table->unsignedInteger('league_id');

to

$table->integer('league_id');

以上是关于1215一般错误:无法添加外键约束Laravel的主要内容,如果未能解决你的问题,请参考以下文章

1215一般错误:无法添加外键约束Laravel

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

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

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

SQLSTATE[HY000]:一般错误:1215 无法在表用户中添加外键约束 Laravel

Laravel - 1215 无法添加外部约束