Home > database >  Problem with making relationships using Laravel migrations
Problem with making relationships using Laravel migrations

Time:11-26

I have problem with Laravel migrations. I want to make a relationship between two tables but

I am getting error General error: 1005 Can't create table eshopper.prices (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table prices add constraint pri ces_product_id_foreign foreign key (product_id) references products (id) .

Here is my code. Tables are prices and products.

Prices

public function up()
    {
        Schema::create('prices', function (Blueprint $table) {
            $table->id();
            $table->float('amount');
            $table->unsignedBigInteger('product_id')->unsigned()->index();

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

Products

public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string("title",100);
            $table->text("description");
            $table->timestamps();
        });
    }

enter image description here

NOTE: In my migrations products table is under prices table, I know that the first created table is prices than products and that is error. My question is do I have to put products frst or I can keep same layout(prices first, than products) and change something in code?

CodePudding user response:

it happens because the product_id field type is different from the id field in the products table, try this in the products migration file:

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->unsignedBigInteger('id', true); // true here means it's auto incremental
        $table->string("title",100);
        $table->text("description");
        $table->timestamps();
    });
}

CodePudding user response:

Instead of putting the constraints inside the migration file of price you can put it inside the migration file of products. Since it will create the prices table first before the products the constraints will not be created because the products table is not yet created.

/**
* This is the prices table
*
* it depends in you if you want to define the column here directly without its constraints
* the commented column is optional
*/
public function up()
{
    Schema::create('prices', function (Blueprint $table) {
        $table->id();
        $table->float('amount');
        //$table->unsignedBigInteger('product_id');
        $table->timestamps();
    });
}

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->id();
        $table->string("title",100);
        $table->text("description");
        $table->timestamps();
    });

    // you can define your constraints here
    Schema::table('prices', function(Blueprint $table) {
        $table->foreignId('product_id')
            ->constrained('products')
            ->cascadeOnDelete()
            ->cascadeOnUpdate();

        // if you uncomment the column commented on the prices table instead of the one at above you can use this instead
        $table->foreign('product_id')
            ->references('id')
            ->on('products')
            ->cascadeOnDelete()
            ->cascadeOnUpdate();
    });
}
  • Related