简体   繁体   中英

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 . #sql-9e4_161 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table receivers add constraint receivers_hospital_id_foreign foreign key ( hospital_id ) references 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.

Check that

1) All the foreign keys should match the type of primary key they match. For example, if you use increments for PK make sure that FK has type of unsignedInteger

2) Make sure the order is right. 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. You can't do like that, there should only be one of then (probably 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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