Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sanjivsharma's avatar

errno: 150 "Foreign key constraint is incorrectly formed"

I have many to one relation from Category to Post, and following are from respective migrations:

Posts: public function up() { Schema::create('posts', function (Blueprint $table) {

        $table->increments('id');

        $table->string('title');

        $table->string('body')->nullable();

        $table->integer('cat_id')->unique()->nullable();

        $table->foreign('cat_id')
            ->references('id')->on('post_categories')
            ->onDelete('cascade');

        $table->dateTime('event_at')->nullable();

        $table->timestamps();
    });

Post Categories:

public function up() { Schema::create('post_categories', function (Blueprint $table) {

        $table->increments('id')->unsigned()->unique();

        $table->string('cat_name');

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

}

Models: Post: class Post extends Model {

use SearchableTrait;

protected $searchable = [

    'columns' => [
        'posts.title' => 10,
        'posts.body' => 5,
    ]

];

public function postcategory() {

    return $this->belongsTo(PostCategory::class);

}

}

Post_Category: class PostCategory extends Model { public function posts() {

    return $this->hasMany(Post::class);

}

}

I keep getting this error message when i try migrating(php artisan migrate):

[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1005 Can't create table artgallery.#sql-36c4_160 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table posts add constraint pos ts_cat_id_foreign foreign key (cat_id) references post_categories (id) on delete cascade)

[PDOException] SQLSTATE[HY000]: General error: 1005 Can't create table artgallery.#sql-36c4_160 (errno: 150 "Foreign key constraint is incorrectly formed")

Please guide where I could be committing mistake and what is the solution.

0 likes
10 replies
jlucia's avatar

Check the order of your migrations. If your migrate command is trying to make the posts table before the post_categories table this will occur with MySQL. It seems to go in order of date, oldest to newest. In other words, the cat_id on the the table it is trying to reference should exist.

8 likes
sanjivsharma's avatar

Thanks!! That was helpful. I faced the same error even after changing the order. Then I found that in PostCategories the PK was as follows:

$table->increments('id')->unsigned()->unique();

whereas in Post migration the fk reference was as follows:

$table->integer('cat_id')->unique()->nullable();

Once I included unsigned () , it vanished.

2 likes
abvelin's avatar

hello @sanjivsharma .

your cat_id require an id on a categories table. so make sure that the categories migrations came before posts migrations and the error will disappear

Snapey's avatar

@abarav thanks for the participation, but this question is a) two years old, b) already solved by adding unsigned to the foreign key.

Snapey's avatar

@jlrdw It may be because on mobile screens the date of replies is not visible. The original post contains the '1 year ago' but its not obvious.

attila's avatar

@jlucia That was it, thank you! A table referencing another was created before the reference existed. I've changed the order of the files by modifying the dates and ran the migrations. They work perfect!

Please or to participate in this conversation.