繁体   English   中英

常规错误:1005无法创建表errno:150“外键约束格式错误”)

[英]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 TABLE receivers添加约束receivers_hospital_id_foreign外键( hospital_id )引用hospitalsid ))

{
    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();
    });

}

我看不到memberblood_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM