Answer the question
In order to leave comments, you need to log in
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();
});
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`))
Answer the question
In order to leave comments, you need to log in
Types $table->bigIncrements('id'); and $table->integer('pechnik_id')->unsigned(); do not match
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question