M
M
Maxim Volkov2020-10-09 14:24:27
Laravel
Maxim Volkov, 2020-10-09 14:24:27

Laravel, migrations. Error errno: 150 "Foreign key constraint is incorrectly formed", how to fix?

Hello!

I'm trying to create three tables in the database, two of which are related by the id key - with the first one. According to the documentation, laravel created the migration files:

Schema::create('pechniks', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('photo', 100)->nullable();
            $table->boolean('active')->default(true);
            $table->string('city')->nullable();
            $table->string('info')->nullable();
            $table->string('phone')->nullable();
            $table->string('email')->nullable();
            $table->text('description')->nullable();
            $table->timestamps();
        });

        Schema::create('images', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('pechnik_id')->unsigned();
            $table->foreign('pechnik_id')->references('id')->on('pechniks');
            $table->string('img');
            $table->timestamps();
        });

        Schema::create('diplomas', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('pechnik_id')->unsigned();
            $table->foreign('pechnik_id')->references('id')->on('pechniks');
            $table->string('img');
            $table->timestamps();
        });

However, when running the migrations, an error message occurs.
SQLSTATE[HY000]: General error: 1005 Can't create table `test`.`images` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `images` add constraint `images_pechnik_id_foreign` foreign key (`pechnik_id`) references `pechniks` (`id`))


I believe that something is wrong with foreign keys - this is indicated by the error message. But what is the reason and how to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2020-10-09
@voland700

Types $table->bigIncrements('id'); and $table->integer('pechnik_id')->unsigned(); do not match

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question