![](/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.