I have a ‘posts’ table and an ‘arrival’ table which references ‘flightno’ (in text string format) as a foreign key. However, when I run the Laravel migration I get the dreaded error:
[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1005 Can't create table
atc
.#sql-2350_84
(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter tablearrival
add constraintarrival_flightno_foreign
foreign key (flightno
) referencesposts
(flightno
))[PDOException] SQLSTATE[HY000]: General error: 1005 Can't create table
atc
.#sql-2350_84
(errno: 150 "Foreign key constraint is incorrectly formed")
Posts
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('flightno');
$table->string('flighttype');
$table->string('toa');
$table->string('doa');
$table->string('runway');
$table->string('route');
$table->string('parking');
$table->timestamps();
});
Arrival
Schema::create('arrival', function (Blueprint $table) {
$table->increments('id');
$table->string('flightno');
$table->string('cleaning');
$table->string('rampservice');
$table->string('waste');
$table->string('deicing');
$table->foreign('flightno')->references('flightno')->on('posts')->onDelete('cascade');
$table->timestamps();
});