簡體   English   中英

刷新遷移時出現外鍵約束錯誤 - Laravel

[英]Foreign key constraint error when refreshing migrations - Laravel

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

我想這與戰斗表有關。

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

我有同樣的問題,Marcin的答案有效但我也發現另一種選擇是使用fresh而不是refresh ,在這種情況下你不需要刪除外鍵。

我不知道這是否是更新的解決方案將幫助面臨同樣問題的人:

在您的遷移功能中使用它
Schema::disableForeignKeyConstraints();

我通常創建最后一個遷移或名稱,它將被排序為最后一個(因為遷移命令按順序運行遷移文件夾下的文件)類似於2099_12_32_AlterTablesCreateForeignKeysmigration ,其中在up函數中我為每個表指定了所有密鑰,最后啟用了外部鍵約束Schema::enableForeignKeyConstraints(); 然后在down我只是禁用它們Schema::disableForeignKeyConstraints(); 允許重置截斷表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM