0

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');
        });
    }
Ryan Sacks
  • 498
  • 1
  • 8
  • 38
  • 1
    what order are these migrations running in? as they should have a timestamp prefixed to the name which is the order – lagbox Dec 01 '20 at 19:44
  • 1
    Ensure that migrations are running in the order : users, employer_profiles, job_posts, employer_profile_job_post. Since each migration file has timestamp prefixed the order of the migration files should be descending to match the migrations run in the order – Donkarnash Dec 01 '20 at 19:51
  • Does this answer your question? [Migration: Cannot add foreign key constraint](https://stackoverflow.com/questions/22615926/migration-cannot-add-foreign-key-constraint) – miken32 Dec 01 '20 at 20:10
  • employer_profile_job_post migration was running before job_posts migration. After changing the date/order it worked! – Ryan Sacks Dec 01 '20 at 20:43

0 Answers0