无法使用 Laravel 5.2 中的外键迁移

Cannot migrate with foreign key in Laravel 5.2

我想创建两个 table usersroles。我的代码如下:

2014_10_12_000000_create_users_table.php

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->integer('role')->unsigned();
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();

            $table->foreign('role')
                ->references('id')
                ->on('roles');
        });
    }

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

2016_03_09_004256_create_roles_table.php

<?php

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

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

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

当我运行php artisan migrate出现以下错误。

[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1005 Can't create table trucking.#sql-1240_44 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table users add constraint u sers_role_foreign foreign key (role) references roles (id))

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

您应该确保,您的 roles table 迁移是 运行 在 users table 迁移之前。默认情况下,在 Laravel 中你创建了 users table 迁移,所以如果你只更改它的代码然后添加 roles 迁移它就不会工作。您应该更改 usersroles 迁移文件名以确保 roles table 迁移文件开头的时间戳在 users table 之前.

例如你可能遇到这样的情况:

2014_10_12_000000_create_users_table.php
2015_10_12_123552_create_roles_table.php

并且您应该将文件重命名为这样:

2015_10_12_123652_create_users_table.php
2015_10_12_123552_create_roles_table.php

当然,我假设您仅在开发期间使用这些迁移,尚未投入生产。

 public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('firstname');
            $table->string('lastname');
            $table->text('slug');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->decimal('fidbonus', 6, 2);
            $table->rememberToken();
            $table->timestamps();
        });
        Schema::create('Userinfos', function (Blueprint $table) {
           $table->increments('id');
           $table->integer('User_id')->unsigned();
           $table->foreign('User_id')->references('id')->on('Users');
           $table->string('address');
           $table->string('address2');
           $table->text('city');
           $table->string('zip');
           $table->string('country');
           $table->string('Phone');
           $table->timestamps();
       });
       Schema::create('password_resets', function (Blueprint $table) {
           $table->string('email')->index();
           $table->string('token')->index();
           $table->timestamp('created_at');
       });
    }
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('firstname');
        $table->string('lastname');
        $table->text('slug');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->decimal('fidbonus', 6, 2);
        $table->rememberToken();
        $table->timestamps();
    });
    Schema::create('Userinfos', function (Blueprint $table) {
       $table->increments('id');
       //$table->integer('User_id')->unsigned();
       $table->unsignedInteger('User_id');
       //$table->foreign('User_id')->references('id')->on('Users');
         $table->foreign('User_id')->references('id')->on('Users')->onDelete('cascade')->onUpdate('cascade');
       $table->string('address');
       $table->string('address2');
       $table->text('city');
       $table->string('zip');
       $table->string('country');
       $table->string('Phone');
       $table->timestamps();
   });
   Schema::create('password_resets', function (Blueprint $table) {
       $table->string('email')->index();
       $table->string('token')->index();
       $table->timestamp('created_at');
   });
}

尝试以上 code.Its 工作正常