【问题标题】:Foreign key constraint error when refreshing migrations - Laravel刷新迁移时外键约束错误 - Laravel
【发布时间】:2016-03-03 03:00:41
【问题描述】:

我的 Laravel 项目迁移时遇到问题。

由于我对 Laravel 还很陌生,所以我无法弄清楚。

我想向一个已经存在的表添加一个外键,这可以工作,但是当我刷新我的迁移时,我得到了这个错误:

[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key
constraint fails (SQL: drop table `battles`)

[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key
constraint fails

这些是我目前的迁移:

表项目

class CreateProjectsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('projects', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('body');
            $table->string('tags');
            $table->string('img');
            $table->string('img_tricolor');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();
        });
    }

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

桌战

class CreateBattlesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('battles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('battle_theme');
            $table->boolean('battle_active');
        });
    }

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

为项目中的战斗添加外键

class AddProjectsBattleIdFk extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('projects', function (Blueprint $table) {
            $table->integer('battle_id')->unsigned();
            $table->foreign('battle_id')->references('id')->on('battles')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('projects', function (Blueprint $table) {
            //
        });
    }
}

我想这与战斗表有关。

【问题讨论】:

    标签: php mysql sql laravel laravel-migrations


    【解决方案1】:

    down 方法中,您需要先删除外键:

    CreateProjectsTable

    public function down()
    {
        Schema::table('projects', function (Blueprint $table) {
            $table->dropForeign('projects_user_id_foreign');
        });
        Schema::drop('projects');
    }
    

    AddProjectsBattleIdFk

    public function down()
    {
        Schema::table('projects', function (Blueprint $table) {
            $table->dropForeign('projects_battle_id_foreign');
            $table->dropColumn('battle_id');
        });
    }
    

    【讨论】:

    • 这很有帮助。在我更仔细地阅读您的答案之前,我也没有意识到您在调用 dropForeign 时必须实际使用“官方”奇怪的外键名称。
    【解决方案2】:

    我遇到了同样的问题,Marcin 的回答有效,但我还发现另一个选项是使用 fresh 而不是 refresh,在这种情况下您不需要删除外键。

    【讨论】:

      【解决方案3】:

      我不知道这个更新的解决方案是否会帮助面临同样问题的人:

      在向下迁移函数中使用它
      Schema::disableForeignKeyConstraints();

      我通常创建为最后一个迁移或将其命名为最后一个迁移(因为迁移命令按顺序运行迁移文件夹下的文件)类似于2099_12_32_AlterTablesCreateForeignKeysmigrationup 函数中我为每个指定所有密钥表并在最后启用外键约束@​​987654324@,然后在down 中我只是禁用了它们Schema::disableForeignKeyConstraints(); 以允许重置截断表。

      【讨论】:

        猜你喜欢
        • 2019-12-24
        • 1970-01-01
        • 2018-04-05
        • 2020-01-20
        • 2013-09-10
        • 2020-12-23
        • 2020-04-04
        • 1970-01-01
        • 2022-11-27
        相关资源
        最近更新 更多