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
Using Laravel Socialite to Login to Facebook
PHP - Listing All Directories and Sub-Directories Recursively in Drop Down Menu
Replace Ereg_Replace with Preg_Replace
How to Force an Image Download in the Browser
Upload Images on Twitter Using PHP
Add ".Active" Class to the Current Page's Link in a Menu Using Jquery or PHP
SQL & PHP - Which Is Faster MySQL_Num_Rows() or 'Select Count()'
Laravel Cannot Delete or Update a Parent Row: a Foreign Key Constraint Fails
Syntax Error, Unexpected T_Encapsed_And_Whitespace, Expecting T_String or T_Variable or T_Num_String
Laravel and View Caching in Development -- Can't See Changes Right Away
Php-Sort Array Based on Another Array
How to Set for Specific Directory Open_Basedir
How to Convert an Image to Black and White in PHP
I Have a Base64 Encoded Png, How to Write the Image to a File in PHP
"Class Xxx Is Not a Valid Entity or Mapped Super Class" After Moving the Class in the Filesystem