php - Laravel. errno: 150 "Foreign key constraint is incorrectly formed"
Get the solution ↓↓↓I have a user information table :
Schema::create('user_information', function (Blueprint $table){
$table->increments('id');
$table->string('full_name');
$table->integer('user_id');
$table->string('nationality');
$table->integer('profession_id');
$table->string('university');
$table->string('academic_degree');
$table->string('company')->nullable();
$table->integer('country_id');
$table->string('phone_number');
$table->text('description')->nullable();
$table->integer('age')->nullable();
$table->string('photo_url')->nullable();
$table->integer('company_id')->nullable();
$table->integer('social_network_id')->nullable();
$table->timestamps();
$table->softDeletes();
});
and in other migration i update the table specifically the foreign keys:
Schema::table('user_information', function (Blueprint $table){
$table->unsignedBigInteger('user_id')->change();
$table->unsignedBigInteger('profession_id')->change();
$table->unsignedBigInteger('country_id')->change();
$table->unsignedBigInteger('company_id')->nullable()->change();
$table->unsignedBigInteger('social_network_id')->nullable()->change();
$table->unsignedBigInteger('register_type_id')->nullable()->change();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('profession_id')->references('id')->on('profession');
$table->foreign('country_id')->references('id')->on('country');
$table->foreign('company_id')->references('id')->on('company');
$table->foreign('social_network_id')->references('id')->on('social_network');
$table->foreign('register_type_id')->references('id')->on('register_type');
});
However when I run the migration I get the next error code:
Migrating: 2021_06_03_155507_update_foreign_keys
In Connection.php line 678:
SQLSTATE[HY000]: General error: 1005 Can't create table `civgeo`.`user_information` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `user
_information` add constraint `user_information_profession_id_foreign` foreign key (`profession_id`) references `profession` (`id`))
In Connection.php line 471:
SQLSTATE[HY000]: General error: 1005 Can't create table `civgeo`.`user_information` (errno: 150 "Foreign key constraint is incorrectly formed")
All tables used as foreign keys have been created before the user information table
Answer
Solution:
OK it was my mistake My error was that in the foreign tables I declared the keys as
this->incrementes('id');
When I should have declared it as
this->id();
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: mysqli::real_connect(): (hy000/2002): connection refused
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.