繁体   English   中英

向迁移添加外键失败-Laravel

[英]Adding foreign key to migration fails - Laravel

我目前正在研究Laravel项目,但遇到了问题。

我正在尝试向迁移添加外键,但是在尝试迁移时似乎失败。

这是我要更改的迁移:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

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

            /*
             * this is the foreign key I'm trying to add
             */
            $table->integer('battle_id')->unsigned();
            $table->foreign('battle_id')->references('id')->on('battles');

            $table->timestamps();
        });
    }

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

这是争斗表的迁移:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

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

这些是重要的文件名:

  • 2015_10_13_120958_create_projects_table.php
  • 2015_11_26_182256_create_battles_table.php

这些是错误消息:

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'scotchbox.#sql3f4_1a4' (errno: 150) (SQL: alter
table `projects` add constraint projects_battle_id_foreign foreign key  (`battle_id`) references `battles`
(`id`))

[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'scotchbox.#sql-3f4_1a4' (errno: 150)

我之前更改迁移没有任何问题。 这是我第一次发生这种情况。 如果我删除外键,一切将照常进行。

我最终找到了自己问题的答案。

迁移无法正常进行,因为'create_projects_table.php'迁移正尝试添加引用不存在的列的列。

因此,迁移顺序显然很重要。

我通过创建新迁移将外键添加到数据库中的“项目”表中来解决此问题。

此迁移在创建“战斗”表之后运行。

或以这种方式进行操作,这应该可行。 请注意,战斗将在项目之前创建,因此将存在。 另外,当您删除它们时,请先删除项目,否则将引发错误

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateBattlesTable extends Migration
{

    public function up()
    {
        Schema::create('battles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('battle_theme');
            $table->boolean('battle_active');
        });
        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');

            /*
             * this is the foreign key I'm trying to add
             */
            $table->integer('battle_id')->unsigned();
            $table->foreign('battle_id')->references('id')->on('battles');

            $table->timestamps();
        });
    }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM