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

Laravel 9 foreignId [Foreign key constraint is incorrectly formed]

I am getting an error while using foreign keys.

Products migration

Schema::create('product', function (Blueprint $table) {
    $table->id(); // Ürün adı
    $table->string('name'); //Ürün adı
    //$table->unsignedBigInteger('category_id');
    $table->foreignId('category_id')->constrained('category');
    $table->timestamps();
});

Category migration

Schema::create('category', function (Blueprint $table) {
    $table->id();
    $table->integer('main_category')->nullable();
    $table->string('name');
    $table->integer('commission');
});

Error

General error: 1005 Can’t create table laravel.product (errno: 150 "Foreign key constraint is incorrectly formed")

I’am using laravel 9

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

>Solution :

You don’t show the order in which you created the migrations but the categories table will need to exist before you create the product table.

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