MySQL Laravel 无法删除或更新父行:外键约束失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47544109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 22:51:12  来源:igfitidea点击:

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

mysqllaravel

提问by BARNOWL

For some reason a user cannot delete a post if it has been liked, it was working before but when I linked posts with likes I have been getting this error, I can't even delete it in Sequel Pro, unless I delete the likes associated with the post first.

出于某种原因,用户无法删除已被喜欢的帖子,它以前可以使用,但是当我将帖子与喜欢的链接链接时,我收到此错误,我什至无法在 Sequel Pro 中删除它,除非我删除相关的喜欢先用帖子。

Error

错误

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (eliapi8.likes, CONSTRAINT likes_post_id_foreignFOREIGN KEY (post_id) REFERENCES posts(id)) (SQL: delete from postswhere id= 149)

SQLSTATE[23000]: 完整性约束违规:1451 无法删除或更新父行:外键约束失败 ( eliapi8. likes, CONSTRAINT likes_post_id_foreignFOREIGN KEY ( post_id) REFERENCES posts( id)) (SQL: delete from postswhere id= 149)

Sequel Pro on Mac

Mac 上的 Sequel Pro

Maybe it's my schema?

也许这是我的架构?

Posts Schema

帖子架构

Schema::create('posts', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->text('body');
    $table->integer('user_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users');
    $table->timestamps();
});

Likes Schema

喜欢架构

Schema::create('likes', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('post_id')->unsigned();
    $table->integer('user_id')->unsigned();
    $table->foreign('post_id')->references('id')->on('posts');
    $table->foreign('user_id')->references('id')->on('users');
    $table->softDeletes();
    $table->timestamps();
});

I can like and unlike a post, but a user cannot delete a post that has been liked.

我可以喜欢和不喜欢帖子,但用户不能删除已喜欢的帖子。

PostController.php

后控制器.php

public function destroy(Post $post){

    $this->authorize('delete', $post);
    $postl =  Post::with('likes')->whereId($post)->delete();

    if ($post->delete()) {
        if($postl){
             return response()->json(['message' => 'deleted']);
        }  
    };

    return response()->json(['error' => 'something went wrong'], 400);
}

回答by Camilo

Yes, it's your schema. The constraint on likes.post_idwill prevent you from deleting records from the poststable.

是的,这是您的架构。的约束likes.post_id将阻止您从posts表中删除记录。

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

一种解决方案可能是onDelete('cascade')likes迁移文件中使用:

Schema::create('likes', function (Blueprint $table) {
    // Some other fields...

    $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.

或者,如果您有 Post 模型和 Like 模型的关系,您可以$post->likes()->delete()在删除帖子本身之前。