简体   繁体   中英

Adding foreign key to migration fails - Laravel

I'm working on a Laravel project at the moment but I have ran into a problem.

I'm trying to add a foreign key to a migration, but it seems to fail when I try to migrate.

This is the migration I'm trying to change:

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

This is the migration of the battles table:

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

These are the file names if they are of importance:

  • 2015_10_13_120958_create_projects_table.php
  • 2015_11_26_182256_create_battles_table.php

And these are the error messages:

[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)

I didn't have any problems changing the migrations before. This is the first time this has happened to me. If I remove the foreign key everything works as it used to.

I eventually found the answer to my own question.

Migrating didn't work because the 'create_projects_table.php' migration was trying to add a column with a reference to a column that didn't exist yet.

So the order of your migrations does matter apparently.

I fixed it by creating a new migration to add the foreign key to the 'projects' table in my database.

This migration runs after the 'battles' table is created.

Or do it this way, which should work. Note that battles will be created before projects, and therefore will exist. Also when you drop them, first remove projects or it will throw an error

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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