Home > Software design >  Laravel SQLSTATE[23000] Cannot delete or update a parent row: a foreign key constraint fails
Laravel SQLSTATE[23000] Cannot delete or update a parent row: a foreign key constraint fails

Time:10-29

I hope you're alright, I just want to put this post because the down() function and drop column and all these things didn't work for me as well.

For example:

public function down()
{
    Schema::table('users', function($table)
    {
        $table->dropColumn(array('description1', 'description2', 'description3'));
    });
}

From this post : Laravel migrate:rollback adding and deleting table columns.

CodePudding user response:

I wrote a simple command line to drop all the tables in order.

Code :

Artisan::command('delete-all', function() {
    (Schema::dropIfExists('bloggers'));
    (Schema::dropIfExists('ticket_labels'));
    (Schema::dropIfExists('labels'));
    (Schema::dropIfExists('tickets'));
    (Schema::dropIfExists('service_admins'));
    (Schema::dropIfExists('services'));
    (Schema::dropIfExists('admins'));
    (Schema::dropIfExists('admin_roles'));
    (Schema::dropIfExists('priorities'));
    (Schema::dropIfExists('statuses'));
    (Schema::dropIfExists('users'));
    (Schema::dropIfExists('failed_jobs'));
    (Schema::dropIfExists('migrations'));
    (Schema::dropIfExists('password_resets'));
    (Schema::dropIfExists('personal_access_tokens'));
    // After deleting all the tables, now executing migrate command-line.
    exec('php artisan migrate');
    $this->comment('Done');
})->purpose('Deleting all the tables on database');

I hope you like :). Enjoy.

CodePudding user response:

The problem may be that you are trying to apply a solution that works on older versions of Laravel, if you are working on a newer version, it is better to use square brackets to define what an array is.

public function down() {
    Schema::table('users', function($table) {
        $table->dropColumn(['description1', 'description2', 'description3']);
    });
}

I recommend that you check the version of laravel you are using and look for answers based on the version.

  • Related