[英]General error: 1005 Can't create table errno: 150 “Foreign key constraint is incorrectly formed”)
Blockquote SQLSTATE[HY000]: General error: 1005 Can't create table
gps
. Blockquote SQLSTATE [HY000]:常规错误:1005无法创建表gps
。#sql-9e4_161
(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter tablereceivers
add constraintreceivers_hospital_id_foreign
foreign key (hospital_id
) referenceshospitals
(id
))#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();
});
}
I don't see member
, blood_groups
table to fully check everything. 我看不到member
, blood_groups
表来完全检查所有内容。
Check that 检查一下
1) All the foreign keys should match the type of primary key they match. 1)所有外键都应匹配它们匹配的主键的类型。 For example, if you use increments
for PK make sure that FK has type of unsignedInteger
例如,如果对PK使用increments
,请确保FK具有unsignedInteger
类型
2) Make sure the order is right. 2)确保订单正确。 You can't create the foreign key if you don't have a table. 如果没有表,则无法创建外键。 For the code, I see it should be something like 对于代码,我认为它应该类似于
Also, I found that in the hospitals
table you have FK ( country_id
) to countries
table and in the countries
table you have FK ( hospital_id
) to hospitals
table. 另外,我发现,在hospitals
表,你有FK( country_id
)到countries
表,并在countries
表,你有FK( hospital_id
)到hospitals
表。 You can't do like that, there should only be one of then (probably country_id
). 您不能那样做,只能是其中之一(可能是country_id
)。 The reason why is because (I don't know which of those are created first, so let say it's hospitals
) when you create hospitals
, you try to make FK to the table that doesn't exists. 原因是因为(我不知道首先创建哪个,所以说是hospitals
)在创建hospitals
,您尝试将FK设置为不存在的表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.