Php 错误号:150“;迁移laravel 6中的外键约束格式不正确
我是拉雷维尔的新手,如果我的英语不好,我很抱歉。我已经创建了迁移文件并运行了它Php 错误号:150“;迁移laravel 6中的外键约束格式不正确,php,laravel,migration,Php,Laravel,Migration,我是拉雷维尔的新手,如果我的英语不好,我很抱歉。我已经创建了迁移文件并运行了它 创建\u colleges\u table.php 公共职能 { 创建_majors_table.php 公共职能 { 然后我生成update_colleges_table.php,运行它,得到如下错误: Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table `learn_larave
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table `learn_laravel_crud`.`#sql-1558_e3` (errno:
150 "Foreign key constraint is incorrectly formed") (SQL: alter table `colleges` add constraint `colleges_major_id_foreign` foreign key (`major_id`) references `majors` (`id`))
这是我在update\u colleges\u table.php中的代码
公共职能
要创建
外键
,子列
的数据类型必须与父列
完全匹配
既然majors.id
是一个大增量
即无符号大整数
,那么学院。major\u id
也需要是一个大整数
,而不是整数
所以改变吧
至
我遵循了这一点,但仍然是错误,错误消息相同:illumb\Database\QueryException:SQLSTATE[HY000]:一般错误:1005无法创建表
learn\u laravel\u crud
\sql-1558\u f3
(错误号:150“外键约束格式不正确”)(SQL:alter tablecolleges
add constraintcolleges\u major\u id\u foreign
外键(major\u id
)引用majors
(id
)最后我知道我的错误是什么了,你的建议是对的,在数据类型的选择上出现了错误
Schema::create('majors', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('major_name');
$table->timestamps();
});
}
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table `learn_laravel_crud`.`#sql-1558_e3` (errno:
150 "Foreign key constraint is incorrectly formed") (SQL: alter table `colleges` add constraint `colleges_major_id_foreign` foreign key (`major_id`) references `majors` (`id`))
{
Schema::table('colleges', function (Blueprint $table){
$table->foreign('major_id')->references('id')->on('majors');
});
}
$table->integer('major_id');
$table->unsignedBigInteger('major_id');