why laravel schema reply:
SQLSTATE[HY000]: General error: 1005 Can't create table
employee_management
.employees
(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter tableemployees
add constraintemployees_city_id_foreign
foreign key (city_id
) referencescity
(id
))PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table
employee_management
.employees
(errno: 150 "Foreign key constraint is incorrectly formed")")
My Table:
Schema::create('employees', function (Blueprint $table) {
$table->increments('id', true);
$table->string('lastname', 60);
$table->string('firstname', 60);
$table->string('middlename', 60);
$table->string('address', 120);
$table->integer('city_id')->unsigned();
$table->integer('state_id')->unsigned();
$table->integer('country_id')->unsigned();;
$table->foreign('city_id')->references('id')->on('city');
$table->foreign('state_id')->references('id')->on('state');
$table->foreign('country_id')->references('id')->on('country');
$table->char('zip', 10);
$table->integer('age')->unsigned();
$table->date('birthdate');
$table->date('date_hired');
$table->integer('department_id')->unsigned();
$table->integer('division_id')->unsigned();
// $table->integer('company_id')->unsigned();
$table->foreign('department_id')->references('id')->on('department');
$table->foreign('division_id')->references('id')->on('division');
// $table->foreign('company_id')->references('id')->on('company');
$table->string('picture', 60);
$table->timestamps();
$table->softDeletes();
});
My second table for city:
Schema::create('city', function (Blueprint $table) {
$table->increments('id', true);
$table->integer('state_id')->unsigned();
$table->foreign('state_id')->references('id')->on('state');
$table->string('name', 60);
$table->timestamps();
$table->softDeletes();
});
P.S. My Laravel version is 8.12
The reason is you are trying to migrate employee
table before creating city
table and this matters because your employee
table depends on the city
table by foreign key.
To solve that first try to migrate city table.