mirsahib
3 months ago
373
3
Laravel

Error while making a non primary key foreign key (errno: 150 "Foreign key constraint is incorrectly formed)

Posted 3 months ago by mirsahib

I have the following relationship diagram https://imgur.com/RhR5enY (green text means primary key and yellow text mean foreign key)

I am getting the following error while migration

errno: 150 "Foreign key constraint is incorrectly formed

Tenant Table

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTenantsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tenants', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('nid');
            $table->string('nid_img');
            $table->string('phone');
            $table->string('exp_rent');
            $table->string('paid_rent');
            $table->string('dues');
            $table->string('pay_date');
            $table->string('comment');
            $table->integer('hrid'); //home and room no
            $table->boolean('status');
            $table->date('exit');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tenants');
    }
}

Mother Meter Table

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateMotherMetersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */

    //protected $fillable = ['meter_number','hrid','type','consume_unit','bill_amount','year','month','pay_status'];

    public function up()
    {
        Schema::create('mother_meters', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('meter_number');
            $table->unsignedInteger('assign_hrid');//home or room no
            $table->string('type');
            $table->string('consume_unit');
            $table->string('bill_amount');
            $table->string('year');
            $table->string('month');
            $table->string('pay_status');
            $table->timestamps();


            $table->foreign('assign_hrid')->references('hrid')->on('tenants');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('mother_meters');
    }
}

Sub Meter Table

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSubMetersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */

    //protected $fillable = ['meter_number','hrid','type','prev_reading','curr_reading','consume_unit','bill_amount','year','month','pay_status'];

    public function up()
    {
        Schema::create('sub_meters', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('assign_meter_num');
            $table->unsignedInteger('rid');//home or room no
            $table->string('type');
            $table->string('prev_reading');
            $table->string('curr_reading');
            $table->string('consumeny_unit');
            $table->string('bill_amount');
            $table->string('year');
            $table->string('month');
            $table->string('pay_status');
            $table->timestamps();

            $table->foreign('assign_meter_num')->references('meter_number')->on('mother_meters');
            $table->foreign('rid')->references('assign_hrid')->on('mother_meters');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('sub_meters');
    }
}

Please sign in or create an account to participate in this conversation.