重复列名称Laravel

时间:2016-10-31 10:16:17

标签: php mysql laravel laravel-5 artisan-migrate

我正在 Laravel 创建迁移,当我运行命令php artisan migrate时,该命令会给我以下错误

  

重复列名'user_id'('id'int unsigned not null auto_increment主键,'user_id'int unsigned not null,'user_id'int not null,'order_id_'int unsigned not null,'order_id'int not null )默认字符集utf8 collat​​e utf8_unicode_ci)

这是我的迁移:

<?php

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

class CreateUserOrrderTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
         Schema::create('user_orrder', function (Blueprint $table) {

            $table->increments('id');

            $table->integer('user_id')->unsigned();
            $table->integer('user_id')->references('id')->on('users');

            $table->integer('order_id')->unsigned();
            $table->integer('order_id')->references('id')->on('orders');
        });
    }

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

1 个答案:

答案 0 :(得分:1)

您的代码应为:

$table->foreign('user_id')->references('id')->on('users');

而不是$table->integer('user_id')->references('id')->on('users');

阅读docs