刷新迁移时出现外键约束错误 - Laravel

时间:2015-11-29 11:31:10

标签: php mysql sql laravel laravel-migrations

我在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) {
            //
        });
    }
}

我想这与战斗表有关。

3 个答案:

答案 0 :(得分:8)

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');
    });
}

答案 1 :(得分:3)

我有同样的问题,Marcin的答案有效但我也发现另一种选择是使用tr '\n\r' ' ' < intput-file > output-file 而不是fresh,在这种情况下你不需要删除外键。

答案 2 :(得分:0)

我不知道这是否是更新的解决方案是否可以帮助面临相同问题的人

在向下迁移功能中使用此功能
Schema::disableForeignKeyConstraints();

我通常创建最后一个迁移或将其命名为最后一个排序(因为迁移命令会按顺序运行迁移文件夹下的文件),类似于2099_12_32_AlterTablesCreateForeignKeysmigration,其中在up函数中,我指定了全部我为每个表创建的键,最后都启用了外键约束Schema::enableForeignKeyConstraints();,然后在down中,我只是禁用了它们Schema::disableForeignKeyConstraints();,以允许重置来截断表。