Laravel,列已存在:1060 列名重复

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44012106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:58:56  来源:igfitidea点击:

Laravel ,Column already exists :1060 Duplicate column name

phplaravel

提问by user7379812

I am running laravel version 5.4.22

我正在运行 Laravel 版本 5.4.22

I use php artisan migrate:rollbackin terminal then error message bellow

php artisan migrate:rollback在终端中使用然后错误消息如下

H:\wamp_server\www\cms>php artisan migrate:rollback

[Illuminate\Database\QueryException] SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'is_admin' (SQL: alter table postsadd is_adminint not null)

[PDOException] SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'is_admin'

H:\wamp_server\www\cms>php artisan migrate:rollback

[Illuminate\Database\QueryException] SQLSTATE[42S21]:列已经存在:1060 列名重复'is_admin'(SQL:alter table postsadd is_adminint not null)

[PDOException] SQLSTATE[42S21]:列已存在:1060 列名重复“is_admin”

migration code below

下面的迁移代码

    <?php

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

class AddIsAdminColumnToPostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            //
            $table->integer('is_admin')->unsigned();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('posts', function (Blueprint $table) {
            //

            $table->integer('is_admin');
        });
    }
}

回答by ATechGuy

your code is wrong, you are adding the column in the down function as well, see code below to fix:

您的代码是错误的,您也在 down 函数中添加了列,请参阅下面的代码进行修复:

public function down()
{
    //
    Schema::table('posts', function (Blueprint $table) {
    $table->dropColumn('is_admin');
    });
}

Laravel doc, showing this: https://laravel.com/docs/5.4/migrations#dropping-columns

Laravel 文档,显示这个:https://laravel.com/docs/5.4/migrations#dropping-columns

回答by Muhammad Rehan

I had same problem, Column already exists: Duplicate column name 'created_at'

我有同样的问题,列已经存在:重复列名“created_at”

I think it was due to

我认为这是由于

$table->timepstamps('dob');

my previous code

我以前的代码

public function up()
    {
        Schema::create('authors', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps('dob');
            $table->timestamps();
        });
    }

when I remove 's' form timestamp, like

当我删除 's' 表单时间戳时,例如

 $table->timestamp('dob');

It works fine for me.

这对我来说可以。