在Laravel 5.2中无法使用外键进行迁移

时间:2016-03-09 18:03:14

标签: mysql laravel-5 foreign-keys database-migration laravel-5.2

我想创建两个表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时,会出现以下错误。

  

[照亮\数据库\ QueryException]     SQLSTATE [HY000]:常规错误:1005无法创建表trucking#sql-1240_44(错误号:150“外键约束形成错误”)(SQL:alter table users添加约束ü     sers_role_foreign外键(role)引用rolesid))

     

[PDOException]     SQLSTATE [HY000]:常规错误:1005无法创建表trucking#sql-1240_44(错误号:150“外键约束形成错误”)

3 个答案:

答案 0 :(得分:6)

您应确保roles表迁移在users表迁移之前运行。默认情况下,您在Laravel中创建了users表格迁移,因此,如果您只更改了代码并稍后添加了roles迁移,那么它将无法正常运行。您应该更改usersroles迁移文件名,以确保roles表迁移文件开头的时间戳位于users表之前。

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

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

当然,我假设您仅在开发过程中使用这些迁移,而且还没有进行生产。

答案 1 :(得分:1)

public class Composite extends Picture {
    private ArrayList<Picture> components;

    public Composite (){
    //I have no idea how to chain.
    }
...

答案 2 :(得分:0)

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

尝试上面的代码。它工作正常