Laravel migration错误号:150“外键约束不正确”

问题描述 投票:0回答:1

我有一个'posts'表和一个'arrival'表,它引用'flightno'(文本字符串格式)作为外键。但是,当我运行Laravel迁移时,我遇到了可怕的错误:

[Illuminate \ Database \ QueryException] SQLSTATE [HY000]:一般错误:1005无法创建表atc.#sql-2350_84(错误:150“外键约束形成错误”)(SQL:alter table arrival add constraint arrival_flightno_foreign foreign key(flightno) )参考postsflightno))

[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();
});
laravel laravel-5 laravel-5.4
1个回答
0
投票

在我看来,你忘了放置索引和设置flightno列的长度。这应该工作:

帖子

Schema::create('posts', function (Blueprint $table) {
    // ...
    $table->string('flightno', 30)->index();
    // ...
}); 

到达

Schema::create('arrival', function (Blueprint $table) {
    // ...
    $table->string('flightno', 30)->index();
    // ...
}); 
© www.soinside.com 2019 - 2024. All rights reserved.