Laravel-外键约束格式错误 [英] Laravel - Foreign key constraint is incorrectly formed

查看:71
本文介绍了Laravel-外键约束格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在laravel中创建表'teams'和'competitions',但是当我运行migration命令时,得到以下信息:errno:150外键约束的格式不正确"

I am trying to create the tables 'teams' and 'competitions' in laravel but when I run the migrate command, I get the following: errno: 150 "Foreign key constraint is incorrectly formed"

Schema::create('competitions', function (Blueprint $table) {
                    $table->increments('id');
                    $table->string('name')->unique();
                    $table->string('team_name');
                    $table->foreign('team_name')->references('name')->on('teams')->onDelete('cascade');
                    $table->timestamps();
        });

Schema::create('teams', function (Blueprint $table) {
                    $table->increments('id');
                    $table->string('name')->unique();
                    $table->string('place');
                    $table->string('competition')->nullable();;
                    $table->foreign('competition')->references('name')->on('competitions')->onDelete('set null');
                    $table->timestamps();
});

推荐答案

您必须对主键外键

例如都具有-> nullable()或没有.

eg. both had ->nullable() or not.

还要确保两个表及其列的排序规则相同.

Also, make sure the collation is same for two tables and their columns.

这篇关于Laravel-外键约束格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆