无法迁移外键

Can't Migrate Foreign Key

这是车辆迁移:

public function up()
    {
        Schema::create('Vehicles', function (Blueprint $table) {
            $table->increments('serie');
            $table->string('color');
            $table->integer('power');
            $table->float('capacity');
            $table->float('speed');
            $table->integer('maker_id')->unsigned();
            $table->timestamps();
        });

        Schema::table('Vehicles', function (Blueprint $table){
            $table->foreign('maker_id')->references('id')->on('Makers');
        });
    }

这里是创客迁移:

public function up()
    {
        Schema::create('Makers', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->integer('phone');
            $table->timestamps();
        });
    }

当我 运行 artisan migrate 时,我收到以下错误消息。

 [Illuminate\Database\QueryException]
 SQLSTATE[HY000]: General error: 1005 Can't create table `api`.`#sql-14b0_71
 ` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter t
 able `Vehicles` add constraint `vehicles_maker_id_foreign` foreign key (`ma
 ker_id`) references `Makers` (`id`))



 [PDOException]
 SQLSTATE[HY000]: General error: 1005 Can't create table `api`.`#sql-14b0_71
 ` (errno: 150 "Foreign key constraint is incorrectly formed")

我想创建两个表: Vehicles & Makers 。在车辆中,maker_id 是外键。我从各种来源阅读了一些关于 laravel 迁移的参考资料。但是我找不到解决方案。

注意你的迁移文件顺序,你有两个解决方案:

  1. 更改顺序:先制作 Makers Migrate 文件,然后 Vehicles Migrate
  2. 如果 Makers Migrate 文件在 Vehicles Migrate 文件之后并且您不想更改顺序,请移动此:

    Schema::table('Vehicles', function (Blueprint $table){
        $table->foreign('maker_id')->references('id')->on('Makers');
    });
    

    Makers Migrate文件。

现在我删除了制造商迁移文件和 运行 车辆迁移文件。以下是车辆迁移文件。

public function up()
    {
        Schema::create('Vehicles', function (Blueprint $table) {
            $table->increments('serie');
            $table->string('color');
            $table->integer('power');
            $table->float('capacity');
            $table->float('speed');
            $table->integer('maker_id')->unsigned();
            $table->timestamps();
        });
    }

但是我得到以下错误

PHP Fatal error:  Cannot redeclare class CreateVehicleTable in C:\xampp\htdocs\m
yownapi\database\migrations16_07_05_190215_vehicles_table.php on line 35


  [Symfony\Component\Debug\Exception\FatalErrorException]
  Cannot redeclare class CreateVehicleTable

现在可以正常使用以下代码

创客迁移

<?php

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

class MakersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('Makers', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->integer('phone');
            $table->timestamps();
        });

        Schema::table('Vehicles', function(Blueprint $table){
            $table->foreign('maker_id')->references('id')->on('makers');
        });
    }

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

车辆迁移

<?php

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

class VehiclesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('Vehicles', function (Blueprint $table) {
            $table->increments('serie');
            $table->string('color');
            $table->float('power');
            $table->float('capacity');
            $table->integer('maker_id')->unsigned();
            $table->timestamps();
        });
    }

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

有一种简单的方法可以做到这一点。从您的 Vehicles 迁移中,进行以下更改

public function up()
{
    Schema::create('Vehicles', function (Blueprint $table) {
        $table->increments('serie');
        $table->string('color');
        $table->integer('power');
        $table->float('capacity');
        $table->float('speed');
        $table->integer('maker_id')
        $table->foreign('maker_id')
              ->references('id')->on('Makers');
        $table->timestamps();
    });
}

并且由于您将在 Makers 中的 id 列中添加值,Laravel 已经得到了您的支持,所以真的没有必要在那里添加 unsigned() 属性 。加进去还是可以的

希望我已经回答了你的问题。 For More Details