Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel ,Column already exists :1060 Duplicate column name

Tags:

php

laravel

I am running laravel version 5.4.22

I use php artisan migrate:rollback in terminal then error message bellow

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 posts add is_admin int not null)

[PDOException] SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name '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');
        });
    }
}
like image 509
Rasel Avatar asked Feb 05 '23 10:02

Rasel


2 Answers

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

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

like image 58
ATechGuy Avatar answered Feb 08 '23 15:02

ATechGuy


I had same problem, Column already exists: Duplicate column name '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

 $table->timestamp('dob');

It works fine for me.

like image 30
Muhammad Rehan Avatar answered Feb 08 '23 16:02

Muhammad Rehan