我有一个'posts'表和一个'arrival'表,它引用'flightno'(文本字符串格式)作为外键。但是,当我运行Laravel迁移时,我遇到了可怕的错误:
[Illuminate \ Database \ QueryException] SQLSTATE [HY000]:一般错误:1005无法创建表
atc
.#sql-2350_84
(错误:150“外键约束形成错误”)(SQL:alter tablearrival
add constraintarrival_flightno_foreign
foreign key(flightno
) )参考posts
(flightno
))[PDOException] SQLSTATE [HY000]:常规错误:1005无法创建表
atc
.#sql-2350_84
(错误:150“外键约束形成错误”)
帖子
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();
});
到达
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();
});
在我看来,你忘了放置索引和设置flightno
列的长度。这应该工作:
帖子
Schema::create('posts', function (Blueprint $table) {
// ...
$table->string('flightno', 30)->index();
// ...
});
到达
Schema::create('arrival', function (Blueprint $table) {
// ...
$table->string('flightno', 30)->index();
// ...
});