I have an employer_profiles table, a job_posts table and an employer_profile_job_post table. I'm trying to make a relationship and add the foreign keys but I keep getting this error and I'm not sure why.
SQLSTATE[HY000]: General error: 1005 Can't create table `highrjobsadminlte`.`employer_profile_job_post` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `employer_profile_job_post` add constraint `employer_profile_job_post_job_post_id_foreign` foreign key (`job_post_id`) references `job_posts` (`id`))
create_employer_profile_job_post_table.php:
public function up()
{
Schema::create('employer_profile_job_post', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('employer_profile_id')->unsigned()->index();
$table->bigInteger('job_post_id')->unsigned()->index();
$table->timestamps();
$table->foreign('employer_profile_id')->references('id')->on('employer_profiles');
$table->foreign('job_post_id')->references('id')->on('job_posts');
});
}
create_employer_profiles_table.php:
public function up()
{
Schema::create('employer_profiles', function (Blueprint $table) {
$table->id();
$table->bigInteger('user_id')->index()->unsigned();
$table->string('company_name');
$table->string('phone',30)->nullable();
$table->string('street');
$table->string('city', 50);
$table->string('country', 80);
$table->string('zip_postal', 25);
$table->string('province_state', 50);
$table->string('number_of_employees');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
create_job_posts_table.php:
public function up()
{
Schema::create('job_posts', function (Blueprint $table) {
$table->id();
$table->bigInteger('user_id')->index()->unsigned();
$table->bigInteger('employer_profile_id')->index()->unsigned();
$table->string('job_title');
$table->longText('job_description');
$table->string('salary', 20);
$table->string('employment_type', 20)->nullable();
$table->string('location_name', 100);
$table->tinyInteger('is_active')->default(0);
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('employer_profile_id')->references('id')->on('employer_profiles');
});
}