Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 错误号:150“;迁移laravel 6中的外键约束格式不正确_Php_Laravel_Migration - Fatal编程技术网

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

我是拉雷维尔的新手,如果我的英语不好,我很抱歉。我已经创建了迁移文件并运行了它

  • 创建\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_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 table
    colleges
    add constraint
    colleges\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');