加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – 刷新迁移时的外键约束错误 – Laravel

发布时间:2020-12-14 19:35:51 所属栏目:大数据 来源:网络整理
导读:我在Laravel项目中遇到迁移问题. 因为我对Laravel相当新,所以我无法理解. 我想将一个外键添加到现有的表中,这样可行,但是当我刷新迁移时,我收到此错误: [IlluminateDatabaseQueryException]SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot
我在Laravel项目中遇到迁移问题.

因为我对Laravel相当新,所以我无法理解.

我想将一个外键添加到现有的表中,这样可行,但是当我刷新迁移时,我收到此错误:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读