Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (Laravel)

I keep getting this error. I understood it’s a problem with the foreign key but i really can’t understand what it’s not working properly. I’m trying to make a many to many relationship with a bridge table. Please, if you can help me solve this! Here you can see the migrations:

Table dresses:

<?php

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

class Dress extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('dresses', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('dress_id');
            $table->string('name')->unique();
            $table->string('brand');
            $table->decimal('price');
            $table->string('fabric');
            $table->string('size');
            $table->string('type');
            $table->string('wash_instruction');
            $table->string('product_details');
            $table->string('fit');
            $table->rememberToken();;
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

Table Wishlist:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?php

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

class Wishlist extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('wishlists', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('wishlist_id');
            $table->foreign('wishlist_id')
                ->references('user_id')
                ->on('users')->onDelete('cascade');
        });
    }



    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

Bridge table:

<?php

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

class CreateWishlistDressTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('wishlist_dress', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('dress_id');
            $table->foreign('dress_id')
                ->references('id')
                ->on('dresses')->onDelete('cascade');

            $table->unsignedBigInteger('wishlist_id');
            $table->foreign('wishlist_id')
                ->references('id')
                ->on('wishlists')->onDelete('cascade');

            $table->timestamps();
        });
    }

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

>Solution :

How is your users migration set up? Do you have a column named users_id in the users migration file?

Usually, only the id column is used, which is generated from $table->id() in the users migrations file. I believe your migrations should work if you change from users_id to id like following:

<?php

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

class Wishlist extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('wishlists', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('wishlist_id');
            $table->foreign('wishlist_id')
                ->references('id') //Changed from user_id to id
                ->on('users')->onDelete('cascade');
        });
    }



    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading