2

I am getting the following error when running migrations:

PDOException::("SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'role_id'")

<?php

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        if(!Schema::hasTable('users')) {
            Schema::create('users', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name');
                $table->string('email')->unique();
                $table->timestamp('email_verified_at')->nullable();
                $table->string('password');
                $table->rememberToken();
                $table->timestamps();
            });
        }

        Schema::table('users', function (Blueprint $table) {
            $table->integer('role_id')->unsigned();
            $table->string('first_name')->nullable();
            $table->string('middle_name')->nullable();
            $table->string('last_name')->nullable();
            $table->string('city')->nullable();
        });
    }

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

        Schema::table('users', function (Blueprint $table) {
        $table->dropColumn('role_id');
         });
    }
}

I have deleted most of the migrated tables as it gives duplication issues. Could that be related to my existing problem ?

6
  • did you deleted this migration before you ran it? Commented Oct 29, 2018 at 7:40
  • why you are not running new migration file for editing the user table Commented Oct 29, 2018 at 7:54
  • seems to be you already have the table in your database, or you have already run the migration. Commented Oct 29, 2018 at 7:57
  • 2
    Better if you could post DB structure Commented Oct 29, 2018 at 7:57
  • that means you already added that column Commented Oct 29, 2018 at 9:09

1 Answer 1

2

try running these commands in your terminal:

  1. composer dump-autoload // updates whatever you changed in your migration
  2. php artisan migrate:fresh // migrates migration from the start

if these doesn't not work, post your column structure so we can understand more about your problem.

Sign up to request clarification or add additional context in comments.

4 Comments

it did not work. Now the user table is empty and i cannot loggin. Update: i have again registered as a user and the login works now.
Now the current situation is like before all the migrations were created. But Those all old tables exists in the editor. If i have to create all migrations fresh, it will be a waste of time for me. Could you advise me whether i could use all the existing migrations in the editor.
Somehow Migrations worked now without error. I didnt had to create all those old migrations from the scratch fortunately.
if you have a seeder, then use this command php artisan migrate:fresh --seed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.