【问题标题】:can't delete or update item in laravel无法删除或更新 laravel 中的项目
【发布时间】:2021-12-08 08:04:45
【问题描述】:

当我尝试在 laravel 中删除项目时,我收到了这条消息

SQLSTATE[23000]:完整性约束违规:1451 无法删除或更新父行:外键约束失败(larblog.comments,CONSTRAINT comments_article_id_foreign FOREIGN KEY (article_id) REFERENCES articles (id)) (SQL: 从articles 中删除id = 2)

这是我的删除功能

 public function DeleteArticle($id){
        $article = Article::find($id);
        $article->delete();
        return redirect("article");
    }

这个创建文章表代码

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateArticle extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('body');
            $table->timestamps();
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users');

        });
    }

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

这会创建 cmets 表代码

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateComments extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->text('comment');
            $table->timestamps();
            $table->integer('article_id')->unsigned();
            $table->foreign('article_id')->references('id')->on('articles');
        });
    }

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

我尝试使用此解决方案,但没有奏效

$table->foreign('article_id')->references('id')->on('articles')->onUpdate('cascade')- 
>onDelete('cascade');

【问题讨论】:

  • 你把这段代码放在哪里了? $table->foreign('article_id')->references('id')->on('articles')->onUpdate('cascade')->onDelete('cascade');
  • public function up() { Schema::create('cmets', function (Blueprint $table) { $table->increments('id'); $table->text('comment' ); $table->timestamps(); $table->integer('article_id')->unsigned(); $table->foreign('article_id')->references('id')->on('articles ')->onUpdate('cascade')->onDelete('cascade'); }); }
  • 您引用的外键必须是bigIntegerunsignedBigInteger,如$table-&gt;unsignedBigInteger('article_id'); 中所述,如laravel.com/docs/8.x/migrations#foreign-key-constraints 中所述

标签: php laravel


【解决方案1】:

您知道,一旦删除文章,您的 cmets 将依赖文章,然后评论的关系就会中断,因此您要么首先删除该文章的所有评论,要么将外键设置为 null 为该文章的所有评论
所以你为什么需要更新你的 mygration

$table-&gt;foreign('article_id')-&gt;references('id')-&gt;on('articles')-&gt;onDelete('cascade');

$table-&gt;integer('article_id')-&gt;unsigned()-&gt;nullable();

$table-&gt;foreign('article_id')-&gt;references('id')-&gt;on('articles')-&gt;onDelete('set null');

【讨论】:

  • iam 已将 create_cmets.php 文件中迁移文件夹中的代码更新为 $table->foreign('article_id')->references('id')->on('articles')->onDelete ('级联');但没有用,我在上面帖子的末尾告诉了这个,现在尝试使用 $table->foreign('article_id')->references('id')->on('articles')->onDelete('设置为空');但也没有用我做错了什么?
  • 完成此操作后,您是否需要再次运行迁移,如 php artisan migrate:fresh 请注意此闪存旧数据,因此如果您需要数据,则需要手动更改到数据库中
  • 谢谢 raghav,当我运行此订单时,它已成功运行 (artisan migrate:fresh),祝你好运:)
猜你喜欢
  • 2020-01-10
  • 1970-01-01
  • 2020-05-14
  • 2023-03-08
  • 1970-01-01
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多