简体   繁体   English

无法迁移外键

[英]Can't Migrate Foreign Key

Here is Vehicles 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();
        });

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

Here is Makers Migrate : 这是Makers Migrate:

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

When I run artisan migrate , I got following error messages. 运行artisan migration时,出现以下错误消息。

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

I want to create two tables: Vehicles & Makers . 我想创建两个表:Vehicles&Makers。 In vehicles , maker_id is foreign key . 在车辆中,maker_id是外键。 I read the some references for laravel migration from various sources. 我从各种来源阅读了一些有关laravel迁移的参考。 But I can't find solution. 但是我找不到解决方案。

Pay attention to your migration files order, you have two solutions: 注意您的迁移文件顺序,您有两种解决方案:

  1. Change order: make Makers Migrate file first then Vehicles Migrate . 更改顺序:首先使Makers Migrate文件,然后使Vehicles Migrate
  2. If Makers Migrate file comes after Vehicles Migrate file and you don't want to change order, move this: 如果Makers Migrate文件位于Vehicles Migrate文件之后,并且您不想更改订单,请移动以下命令:

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

    To Makers Migrate file. Makers Migrate文件。

Now I delete makers migrate file and I run vehicles migrate file . 现在,我删除制造商的迁移文件,并运行车辆迁移文件。 Below is vehicles migrate file. 以下是车辆迁移文件。

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

But I got the below errors 但是我得到了以下错误

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


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

Now it is work properly with below codes 现在可以使用以下代码正常工作

Makers Migrate 厂商迁移

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

Vehicles Migrate 车辆迁移

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

There is a simple way to do this. 有一个简单的方法可以做到这一点。 From your Vehicles migration, make the following changes 在您的车辆迁移中,进行以下更改

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

And since you will be adding the values form the id column in Makers , Laravel already has got your back, so there is really no need to add the unsigned() property there. 并且由于您将在Makers中id列中添加值,因此Laravel已经支持了您,因此实际上不需要在其中添加unsigned()属性。 Its still Okay if you add it there. 如果在此添加它仍然可以。

Hope I have answered your question. 希望我回答了你的问题。 For More Details 更多细节

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

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