簡體   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