【问题标题】:Laravel Foreign Key Constraint Errors when deleting Parent with data in child of childLaravel 外键约束错误删除父级时的子级数据
【发布时间】:2018-11-07 15:36:21
【问题描述】:

这就是它的设置方式。

  • 一个步骤有一个类别
  • 一个类别有一个部分
  • 一个部分有多个问题

以为我的模型文件中的所有内容都可以正常工作,但不幸的是,我的外键遇到了完整性错误。

我的模型被命名为:

Step.php
StepCategory.php
StepSection.php
StepQuestion.php

我不确定如何建立我的“多对一”关系,或者什么属于什么。

我的数据库表名如下:

steps

step_categories

step_sections

step_questions

我在删除任何内容的父条目时遇到此错误...如果我删除一个步骤,它应该删除一个类别、部分以及与之相关的问题。如果我删除一个类别,它应该删除部分和问题。如果我删除一个部分,它会删除所有问题。

在我的step_categories 表中,我有一个名为step_id 的外键列。

在我的step_sections 表中,我有一个名为category_id 的外键列。

在我的step_questions 表中,我有一个名为section_id 的外键列。

我的step_categories 表上的外键名称是step_categories_step_id_foreign

我的step_sections 表上的外键名称是step_sections_category_id_foreign

我的step_questions 表上的外键名称尚未设置,因为我还没有将其作为网站的一部分。我目前卡在sections部分。

我的StepsController.php 文件中的destroy() 函数是:

public function destroy($id)
{
    // Find the step by the ID
    $step = Step::find($id);

    // Find all the categories and delete them.
    $step->category()->delete();

    // Delete the step
    $step->delete();

    // Flash the Session
    Session::flash('success', 'The step has been successfully deleted.');

    // Return Redirect to the index page
    return redirect()->route('steps.index');
}

这是我的模型到目前为止的样子......这是我所得到的...... :(

Step.php

class Step extends Model
{
    // Tell the model what table to use
    protected $table = 'steps';

    // Define the relationship between steps and step categories
    public function category() {
        return $this->hasMany('App\StepCategory');
    }

}

StepCategory.php

class StepCategory extends Model
{
    // Tell the model what table to use
    protected $table = 'step_categories';

    // Define the relationship between step categories and steps
    public function step() {
        return $this->hasOne('App\Step', 'id', 'step_id');
    }

    // Define the relationship between steps categories and sections
    public function section() {
        return $this->hasMany('App\StepSection');
    }

}

还有我的StepSection.php

class StepSection extends Model
{
    // Tell the model what table to use
    protected $table = 'step_sections';

    // Define the relationship between step sections and step categories
    public function category() {
        return $this->hasOne('App\StepCategory', 'id', 'category_id');
    }
}

所以这是我认为可以解决问题的尽可能多的合法信息。如果您需要更多信息来帮助我建立这些关系,请告诉我。

到目前为止,我可以删除一个步骤,它会删除该类别,只要该类别没有附加任何部分。但是,如果 Step 或 Category 附加了任何部分,我将无法删除它。 :/ 不知道怎么回事。

【问题讨论】:

  • 您是否在外键迁移中添加了 ondelete - 级联?

标签: php laravel laravel-5 eloquent foreign-keys


【解决方案1】:

看起来您的问题是因为您的表,因此请确保当您在迁移中声明外键时,您正在添加在删除/更新这些行时应该执行的操作。例如:

在您的 step_categories 上,外键“step_id”应该是这样的:

$table->foreign('step_id')->references('id')->on('step')->onDelete('cascade')->onUpdate('cascade');

您应该对所有表格执行以下相同操作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-15
    • 2011-02-24
    • 2019-12-24
    • 1970-01-01
    • 2014-01-17
    • 2016-07-16
    • 2019-04-26
    相关资源
    最近更新 更多