![](/img/trans.png)
[英]General error: 1005 Can't create table (errno: 150 "Foreign key constraint is incorrectly formed")
[英]General error: 1005 Can't create table errno: 150 “Foreign key constraint is incorrectly formed”)
Blockquote SQLSTATE [HY000]:常規錯誤:1005無法創建表
gps
。#sql-9e4_161
(錯誤:150 “被錯誤地形成的外鍵約束”)(SQL:ALTER TABLEreceivers
添加約束receivers_hospital_id_foreign
外鍵(hospital_id
)引用hospitals
(id
))
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamp('dob');
$table->string('profile_image');
$table->string('profession')->nullable();
$table->string('email')->unique();
$table->string('weight');
$table->string('message');
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
{
Schema::create('receivers', function (Blueprint $table) {
$table->increments('id');
$table->integer('member_id')->unsigned()->index();
$table->integer('hospital_id')->unsigned()->index();
$table->timestamps();
$table->softDeletes();
$table->foreign('member_id')->references('id')->on('members');
$table->foreign('hospital_id')->references('id')->on('hospitals');
});
{
Schema::create('hospitals', function (Blueprint $table) {
$table->increments('id');
$table->string('h_name');
$table->integer('country_id')->unsigned()->index();
$table->integer('donate_id')->unsigned()->index();
$table->softDeletes();
$table->timestamps();
$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('donate_id')->references('id')->on('donates');
});
{
Schema::create('donates', function (Blueprint $table) {
$table->increments('id');
$table->date('date');
$table->date('last_bleed');
$table->string('quantity');
$table->string('comments');
$table->integer('blood_group_id')->unsigned()->index();
$table->integer('member_id')->unsigned()->index();
$table->timestamps();
$table->softDeletes();
$table->foreign('blood_group_id')->references('id')-
>on('blood_groups');
$table->foreign('member_id')->references('id')->on('members');
$table->timestamps();
});
}
我看不到member
, blood_groups
表來完全檢查所有內容。
檢查一下
1)所有外鍵都應匹配它們匹配的主鍵的類型。 例如,如果對PK使用increments
,請確保FK具有unsignedInteger
類型
2)確保訂單正確。 如果沒有表,則無法創建外鍵。 對於代碼,我認為它應該類似於
另外,我發現,在hospitals
表,你有FK( country_id
)到countries
表,並在countries
表,你有FK( hospital_id
)到hospitals
表。 您不能那樣做,只能是其中之一(可能是country_id
)。 原因是因為(我不知道首先創建哪個,所以說是hospitals
)在創建hospitals
,您嘗試將FK設置為不存在的表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.