0
votes

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

}
1
Schema::create('countries', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned()->index(); $table->integer('hospital_id')->unsigned()->index(); $table->integer('receiver_id')->unsigned()->index(); $table->timestamps(); $table->foreign('user_id')->references('id')->on('users'); $table->foreign('hospital_id')->references('id')->on('hospitals'); $table->foreign('receiver_id')->references('id')->on('receivers'); }); Ya country ka table haChaudhry Jamshaid
please send your whole migration file, it's unspecific right nowVladyslav Startsev

1 Answers

0
votes

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.