Laravel 外键约束迁移错误

Laravel Migration Error on foreign key constraint

我在 运行 迁移我的 Laravel 项目时遇到问题。当我 运行 artisan migrate 时,它​​停止在外键中。有人可以帮我弄这个吗?我尝试了其他类似问题的解决方案,但它不起作用。

Error Message: SQLSTATE[HY000]: General error: 1005 Can't create table.

airways.#sql-4588_cfb (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: 改变 table flights 添加约束条件 flights_airport_departure_foreign 删除级联时外键 (airport_departure) 引用 airports (airport_code)

flights_table.php

 public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->integer('flight_number')->primary();
            $table->string('airline');
            $table->integer('airport_departure')->unsigned();
            $table->string('departure_time');
            $table->integer('airport_arrival')->unsigned();
            $table->string('arrival_time');
            $table->string('flight_duration');
            $table->timestamps();
        });

        Schema::table('flights', function($table) {
            $table->foreign('airport_departure')
                  ->references('airport_code')->on('airports')
                  ->onDelete('cascade');

                  $table->foreign('airport_arrival')
                  ->references('airport_code')->on('airports')
                  ->onDelete('cascade');
        });
    }


airports_table.php

  public function up()
    {
        Schema::create('airports', function (Blueprint $table) {
            $table->integer('airport_code')->primary();
            $table->string('airport_name');
            $table->string('airport_location');
            $table->string('airport_state');
            $table->timestamps();
        });
    }

回滚迁移,更改迁移顺序,即如果机场 table 是 2014_10_12_0000 并且 flights_table 是 2014_10_12_00001,则更改机场 table 到 0002。

您的外键列类型错误。您没有将 airport_code 设置为无符号整数,但 airport_departure 和 airport_arrival 需要无符号整数。此外,您还需要在创建航班 table 之前创建机场 table,而不是像其他人建议的那样反过来。