Search code examples
laraveleloquentdatabase-migration

Laravel 6 Migration problem: Foreign Key Constraint is incorrectly formed


I know this question has been asked a lot and I have tried all the answers, I just can't seem to debug this problem I am having.

I have a migration called create_product_image_table

class CreateProductImageTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('product_image', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('product_id');
            $table->string('image_url');
            $table->timestamps();

            $table->foreign('product_id')
                ->references('id')
                ->on('products')
                ->onDelete('cascade');
        });
    }

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

The other migration called create_products_table

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('product_name');
            $table->text('product_description');
            $table->decimal('product_cost', 8, 2);
            $table->decimal('product_price', 8, 2);
            $table->bigInteger('unit_sold')->nullable();
            $table->bigInteger('UPC')->nullable();
            $table->unsignedBigInteger('product_image_id')->nullable();
            $table->timestamps();

            $table->foreign('product_image_id')
                ->references('id')
                ->on('product_image')
                ->onDelete('cascade');
        });
    }

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

They both have the same unsigned big integer type. After changing the date when the migration was created and deleted the table and later the database itself, I just can't seem to pass Errno 150 error.

Thanks for your help.

Thomas


Solution

  • Your products_table has a relationship with product_image_table,

    And product_image_table belongs to products_table.

    So you need to define the relation only on product_image_table.

    You need to create products_tablefirst, and it will look like :

    class CreateProductsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('products', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('product_name');
                $table->text('product_description');
                $table->decimal('product_cost', 8, 2);
                $table->decimal('product_price', 8, 2);
                $table->bigInteger('unit_sold')->nullable();
                $table->bigInteger('UPC')->nullable();
                $table->unsignedBigInteger('product_image_id')->nullable();
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('products');
        }
    }
    

    Then you need to create product_image_table, which will look like :

    class CreateProductImageTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('product_image', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->unsignedBigInteger('product_id');
                $table->string('image_url');
                $table->timestamps();
                $table->foreign('product_id')
                    ->references('id')
                    ->on('products')
                    ->onDelete('cascade');
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('product_image');
        }
    }