Laravel Cannot Delete or Update a Parent Row: a Foreign Key Constraint Fails

Laravel Cannot delete or update a parent row: a foreign key constraint fails

Yes, it's your schema. The constraint on likes.post_id will prevent you from deleting records from the posts table.

One solution could be using onDelete('cascade') in the likes migration file:

Schema::create('likes', function (Blueprint $table) {
$table->integer('post_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
});

This way, when a post is deleted, all related likes will be deleted too.

Or, if you have a relationship from the Post model to the Like model, you can $post->likes()->delete() before deleting the post itself.

Laravel 8 Cannot delete or update a parent row: a foreign key constraint fails

first, delete from bid table

bids::where('bids.loan_id', $id)->delete();

After that

Loan_requests::find($id)->delete();
session()->flash('status','Loan Request deleted successfully');
return redirect()->back();

Laravel SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row

The problem is, there is a etablissement_filieres record referencing a record of filieres. Unless you have specified what to do when deleting a record refered by another record as a foreign key, it does not allow you to delete the foreign key record. Ideal way to handle this situation is to specify what to do when deleting a foreign key record. Check the Foreign Key Constraints of Laravel documentation to see the available options. Look at the section where it shows how to define the actions such as,

->onUpdate('cascade')
->onDelete('cascade');

Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails

You are trying to delete a User but an entry in your table UserAnswer is bind to User ( using the foreign key training_user_id ).
You need to delete the UserAnswer, before deleting the User itself.

Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table if exists `questions`)

when you run php artisan:rollback it didn't delete dropForeign

so do this

public function down()
{
Schema::table('questions', function (Blueprint $table) {
$table->dropForeign('user_id');
});
Schema::dropIfExists('questions');
}

OR as you already did rollback you can dropForeign in up function like this

public function up()
{
Schema::create('questions', function (Blueprint $table) {
$table->dropForeign('user_id');

$table->id();
$table->string('title');
$table->text('text');
$table->foreignId('user_id')->constrained('users')->onDelete('cascade');
$table->timestamps();
});
}

then you can run php artisan migrate if foreign already exists then only

Laravel 5.8 / Mysql: Cannot delete or update a parent row a foreign key constraint fails

In migration Class ForeignKeyAnimalTypeServices

Try:

public function down()
{
Schema::disableForeignKeyConstraints();
Schema::table('services', function(Blueprint $table){
$table->dropForeign(['animal_type_id']);
});

Schema::dropIfExists('services');
Schema::enableForeignKeyConstraints();

}


Related Topics



Leave a reply



Submit