0
votes

enter image description herei am trying to run command php artisan migrate:rollback and it throw me the error cannot update or delete a parent row foreign key constraint fails

there is now issue when i run command php artisan migrate it successfully migrate my all tables but when i run rollback command it throw me the error the error is on my purpose_of_visits migration

    public function up()
{
    Schema::create('purpose_of_visits', function (Blueprint $table) {
        $table->increments('id');
        $table->string('purpose', 100);
        $table->string('description', 197);
        $table->integer('speciality_id')->unsigned()->nullable();
        $table->timestamp('created_at')->useCurrent();
        $table->timestamp('updated_at')->useCurrent();
        $table->softDeletes();
        $table->integer('created_by')->unsigned()->nullable();
        $table->integer('updated_by')->unsigned()->nullable();

        $table->foreign('speciality_id')->references('id')->on('specialities')->onDelete('cascade');
        $table->foreign('created_by')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('updated_by')->references('id')->on('users')->onDelete('cascade');
    });
}

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

and my specialities migration:

    public function up()
{
    Schema::create('specialities', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 50);
        $table->string('description',250)->nullable();
        $table->timestamp('created_at')->useCurrent();
        $table->timestamp('updated_at')->useCurrent();
        $table->softDeletes();
        $table->integer('created_by')->unsigned()->nullable();
        $table->integer('updated_by')->unsigned()->nullable();

        $table->foreign('created_by')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('updated_by')->references('id')->on('users')->onDelete('cascade');
    });
}

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

i cant figure out where is the issue even i am using onDelete('cascade') your help will be highly appreciated!

3
do you have down() schema?Mcfaith
yes i have down schemasyed1234
can you edit your post and include the down() schemaMcfaith
i edit please review againsyed1234
on roll back you have to delete specialities table first .. migration is only happen the way its created or addedARUN Madathil

3 Answers

0
votes

Make sure you have speciality_id, created_by and updated_by in the fillable property of your purpose_of_visits model. See docs here.

For example on your model.

protected $fillable = ['speciality_id','created_by','updated_by'];
0
votes

Remove the foreign key constraints of the table before dropping it.

public function down()
{
    Schema::table('purpose_of_visits', function (Blueprint $table) {
        $table->dropForeign(['speciality_id']);
        $table->dropForeign(['created_by']);
        $table->dropForeign(['updated_by']);
    });
    Schema::dropIfExists('purpose_of_visits');
}
0
votes

Sorry For the Late reply There are Two Situation Where this error can be thrown

For Eg: I have tables such as posts, authors

And here is my post table Migration

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('body');
            $table->unsignedInteger('author_id');
            $table->foreign('author_id')->references('id')->on('authors')->onDelete('cascade');
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

and here is my authors table migration

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAuthorsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('authors', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('authors');
    }
}

Situation 1:

now if the posts table migration runs before the authors table migration it my throw the error

Situation 2:

in some cases if you miss unsigned it may throw error

Solution1:

use

$table->unsignedInteger('speciality_id');
$table->unsignedInteger('speciality_id');
                $table->foreign('author_id')->references('id')->on('specialities')->onDelete('cascade');

instead of this

$table->integer('speciality_id')->unsigned()->nullable();
$table->foreign('speciality_id')->references('id')->on('specialities')->onDelete('cascade');

if it again fails use this

try composer dumpautoload

adn then Schema::disableForeignKeyConstraints();

At the beggining of the migration

and at the end

Schema::enableForeignKeyConstraints();

eg: you migration may look like

public function up()
    {
    Schema::disableForeignKeyConstraints();
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
            $table->softDeletes();
        });

Schema::enableForeignKeyConstraints();
    }

and if the same error throws please attach Error Screenshot and commet below

Hope it helps