Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreign key constraint error when refreshing migrations - Laravel

I'm having a problem with migration on my Laravel project.

Since i'm fairly new to Laravel I can't figure it out.

I want to add a foreign key to an already existing table and this works, but when I refresh my migrations I get this error:

[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key
constraint fails (SQL: drop table `battles`)

[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key
constraint fails

These are the migrations I currently have:

Table Projects

class CreateProjectsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('projects', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('body');
            $table->string('tags');
            $table->string('img');
            $table->string('img_tricolor');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('projects');
    }
}

Table Battles

class CreateBattlesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('battles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('battle_theme');
            $table->boolean('battle_active');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('battles');
    }
}

Adding foreign key for battles in projects

class AddProjectsBattleIdFk extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('projects', function (Blueprint $table) {
            $table->integer('battle_id')->unsigned();
            $table->foreign('battle_id')->references('id')->on('battles')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('projects', function (Blueprint $table) {
            //
        });
    }
}

I suppose it has something to do with the battles table.

like image 284
Mathijsvdb Avatar asked Nov 29 '15 11:11

Mathijsvdb


Video Answer


2 Answers

In down methods you need to remove foreign keys first:

In CreateProjectsTable

public function down()
{
    Schema::table('projects', function (Blueprint $table) {
        $table->dropForeign('projects_user_id_foreign');
    });
    Schema::drop('projects');
}

In AddProjectsBattleIdFk

public function down()
{
    Schema::table('projects', function (Blueprint $table) {
        $table->dropForeign('projects_battle_id_foreign');
        $table->dropColumn('battle_id');
    });
}
like image 61
Marcin Nabiałek Avatar answered Oct 17 '22 12:10

Marcin Nabiałek


I had the same problem,the Marcin's answer worked but I also found that another option is using fresh instead of refresh, in that case you don't need to remove foreign keys.

like image 43
Jeison Ortiz Avatar answered Oct 17 '22 12:10

Jeison Ortiz