0

enter image description hereI am new to Laravel. I have created users table previously. employment table has created in migrations. As the next migration I have alter users table to add job_id in employment table to users table. When I run migrations it gives above error.

Note :I need to give job_id in employment table to users table as job_id. soumya is my database name

When I run migrations without the foreign key constraint, it works perfectly.

Migations : employment table

    public function up()
{
    Schema::create('employment', function (Blueprint $table) {
        $table->increments('job_id');
        $table->string('job_title');
        $table->string('job_description')->nullable()->default(NULL);
        $table->string('slug')->unique();


        $table->timestamps();
    });
}
    public function down()
{
    Schema::drop('employment');
}

Migrations altering users table

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->integer('job_id')->after('deleted');
        $table->foreign('job_id')->references('job_id')->on('employment');
    });
}

public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropForeign('users_job_id_foreign');
        $table->dropColumn('job_id');

    });
}
Shyamali
  • 329
  • 7
  • 22
  • the error is purely related to mysql, not to php nor laravel, https://stackoverflow.com/search?q=Foreign+key+constraint+is+incorrectly+formed , once you got the error cause in mysql the edit in php side will be a peace of cake. – hassan Sep 21 '17 at 07:59
  • Possible duplicate of [Migration: Cannot add foreign key constraint in laravel](https://stackoverflow.com/questions/22615926/migration-cannot-add-foreign-key-constraint-in-laravel) – hassan Sep 21 '17 at 08:00
  • Try to change this `$table->integer('job_id')->after('deleted');` to this `$table->unsignedInteger('job_id')->after('deleted');` – Maraboc Sep 21 '17 at 08:01
  • @ Maraboc It give Integrity error. – Shyamali Sep 21 '17 at 08:06
  • 1
    Try it like this `$table->integer('job_id')->unsigned();` !! – Maraboc Sep 21 '17 at 08:12
  • @Maraboc same error – Shyamali Sep 21 '17 at 08:16
  • I think the problem is in the order of migrations try to put the users migration for the forieng key under employment migration and in the same file to test it!! – Maraboc Sep 21 '17 at 08:21
  • @Maraboc Still gives the foreign Key error – Shyamali Sep 21 '17 at 08:25
  • Did you try the last comment with `$table->integer('job_id')->unsigned();` ?? – Maraboc Sep 21 '17 at 08:26
  • When I run the migrations without foreign key constraint, it works without unsigned() – Shyamali Sep 21 '17 at 08:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154967/discussion-between-maraboc-and-shyamali). – Maraboc Sep 21 '17 at 08:37

2 Answers2

0

change your user migration like this

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->unsignedInteger('job_id');
        $table->foreign('job_id')->references('job_id')->on('employment');
    });
}
0

In laravel, tables are migrated on first-come-first-serve basis. Make sure that the table to which your foreign key is referencing is migrated before the table that contains the foreign key.

For that, you can change the names of the migration files such that migration for users tables exist before the migration of the employment table in the project directory.

Ayush28
  • 359
  • 3
  • 18