【问题标题】:Laravel Cannot update or delete a parent row a foreign key constraint failsLaravel 无法更新或删除父行,外键约束失败
【发布时间】:2019-05-25 06:52:34
【问题描述】:

我正在尝试运行命令 php artisan migrate:rollback 并抛出错误无法更新或删除父行外键约束失败

现在当我运行命令 php artisan migrate 时出现问题,它成功迁移了我的所有表,但是当我运行回滚命令时,它向我抛出错误,错误是我的 purpose_of_visits 迁移

    public function up()
{
    Schema::create('purpose_of_visits', function (Blueprint $table) {
        $table->increments('id');
        $table->string('purpose', 100);
        $table->string('description', 197);
        $table->integer('speciality_id')->unsigned()->nullable();
        $table->timestamp('created_at')->useCurrent();
        $table->timestamp('updated_at')->useCurrent();
        $table->softDeletes();
        $table->integer('created_by')->unsigned()->nullable();
        $table->integer('updated_by')->unsigned()->nullable();

        $table->foreign('speciality_id')->references('id')->on('specialities')->onDelete('cascade');
        $table->foreign('created_by')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('updated_by')->references('id')->on('users')->onDelete('cascade');
    });
}

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

和我的专业迁移:

    public function up()
{
    Schema::create('specialities', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 50);
        $table->string('description',250)->nullable();
        $table->timestamp('created_at')->useCurrent();
        $table->timestamp('updated_at')->useCurrent();
        $table->softDeletes();
        $table->integer('created_by')->unsigned()->nullable();
        $table->integer('updated_by')->unsigned()->nullable();

        $table->foreign('created_by')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('updated_by')->references('id')->on('users')->onDelete('cascade');
    });
}

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

即使我正在使用 onDelete('cascade'),我也无法弄清楚问题出在哪里 非常感谢您的帮助!

【问题讨论】:

  • 你有 down() 架构吗?
  • 是的,我有模式
  • 您可以编辑您的帖子并包含 down() 架构
  • 我编辑请重新审核
  • 在回滚时,您必须先删除专业表.. 迁移只会按照创建或添加的方式发生

标签: laravel foreign-keys migration


【解决方案1】:

确保您的 purpose_of_visits 模型的可填充属性中有 speciality_id、created_by 和 updated_by。 See docs here.

例如在您的模型上。

protected $fillable = ['speciality_id','created_by','updated_by'];

【讨论】:

  • 我的可填充数组中有speciality_id
【解决方案2】:

在删除表之前删除表的外键约束。

public function down()
{
    Schema::table('purpose_of_visits', function (Blueprint $table) {
        $table->dropForeign(['speciality_id']);
        $table->dropForeign(['created_by']);
        $table->dropForeign(['updated_by']);
    });
    Schema::dropIfExists('purpose_of_visits');
}

【讨论】:

    【解决方案3】:

    抱歉回复晚了有两种情况会抛出这个错误

    例如: 我有posts, authors等表

    这是我的post 表迁移

    <?php
    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    class CreatePostsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('posts', function (Blueprint $table) {
                $table->increments('id');
                $table->string('title');
                $table->text('body');
                $table->unsignedInteger('author_id');
                $table->foreign('author_id')->references('id')->on('authors')->onDelete('cascade');
                $table->timestamps();
            });
        }
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('posts');
        }
    }
    

    这是我的authors 表迁移

    <?php
    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    class CreateAuthorsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('authors', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name');
                $table->timestamps();
            });
        }
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('authors');
        }
    }
    
    Situation 1:
    

    现在如果posts 表迁移在authors 表迁移之前运行,我会抛出错误

    情况2:

    在某些情况下,如果你错过了unsigned,它可能会抛出错误

    Solution1:
    

    使用

    $table->unsignedInteger('speciality_id');
    $table->unsignedInteger('speciality_id');
                    $table->foreign('author_id')->references('id')->on('specialities')->onDelete('cascade');
    

    而不是这个

    $table->integer('speciality_id')->unsigned()->nullable();
    $table->foreign('speciality_id')->references('id')->on('specialities')->onDelete('cascade');
    

    如果再次失败,请使用此

    try composer dumpautoload
    

    然后 Schema::disableForeignKeyConstraints();

    迁移之初

    最后

    Schema::enableForeignKeyConstraints();

    例如:您的迁移可能看起来像

    public function up()
        {
        Schema::disableForeignKeyConstraints();
            Schema::create('users', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name');
                $table->string('email')->unique();
                $table->timestamp('email_verified_at')->nullable();
                $table->string('password');
                $table->rememberToken();
                $table->timestamps();
                $table->softDeletes();
            });
    
    Schema::enableForeignKeyConstraints();
        }
    

    如果出现同样的错误,请附上错误截图并在下方评论

    希望对你有帮助

    【讨论】:

    • 您好,问题是一样的,请看附件截图错误
    • 我附上了屏幕截图,请查看或者如果你说我可以来聊天吗?
    • 可以分享图片的网址吗
    • 请在命令行中提供完整的镜像和迁移文件名
    猜你喜欢
    • 2018-05-12
    • 2017-06-11
    • 1970-01-01
    • 2021-12-14
    • 2014-11-11
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多