php - 重复的列名 Laravel

标签 php mysql laravel laravel-5 artisan-migrate

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

Duplicate column name 'user_id' ('id' int unsigned not null auto_increment primary key, 'user_id' int unsigned not null, 'user_id' int not null, 'order_id_' int unsigned not null, 'order_id' int not null) default character set utf8 collate 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');
    }
}

最佳答案

你的代码应该是这样的:

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

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

阅读docs .

关于php - 重复的列名 Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40340204/

相关文章:

php mysql 错误 - #1273 - #1273 - 未知排序规则 : 'utf8mb4_general_ci'

php - 检查数据库中是否存在变量或者是否设置了变量

php - 从 CSV 文件导入数据时如何消除 "Notice: Undefined offset: 1"错误

php - ExtJs Grid 中的 JsonStore 数据未加载

php - MYSQL 到带有 PHP 的 CSV,顶部有 4 个空行

mysql - 优化MySQL中的多对多查询

laravel - 如何删除集合中的重复项?

model - laravel 4 across 2 Eloquent with scope

php - 连接到数据库时 Laravel 连接超时

php 解密加密函数并运行它?