SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'user_id' (SQL: alter table `posts` add `user_id` int not null)

Published 6 days ago by towhid

i was create add_user_Id_to_posts - and migrate complete

but when i am trying to another column add_cover_Image_to_posts then show this error SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'user_id' (SQL: alter table posts add user_id int not null) whats the problem ?

Best Answer (As Selected By towhid)
Cronix

If you don't have any real data in the db and can stand to delete it all (all tables), you could try artisan migrate:fresh which will drop all tables, and rerun the migrations from the beginning.

Cronix
Cronix
6 days ago (325,610 XP)

Show your migrations.

bobbybouwmann

Can you show your migrations? It looks like you're trying to add the user_id twice right now! If you show use the migrations we can help you further ;)

towhid
towhid
6 days ago (3,030 XP)
<?php

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

class AddCoverImageToPosts extends Migration
{

    public function up()
    {
        Schema::table('posts', function($table){
          $table->string('cover_image');
        });
    }


    public function down()
    {
        Schema::table('posts',function($table){
          $table->dropColumn('cover_image');
        });
    }
}
bobbybouwmann

So this looks correct. What about the other migrations?

Cronix
Cronix
6 days ago (325,610 XP)

Also check the migrations db table to see if all of the migrations you have run are in there.

towhid
towhid
6 days ago (3,030 XP)

@bobbybouwmann this is my another old migration

<?php

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

class AddUserIdToPosts extends Migration
{

    public function up()
    {
       Schema::table('posts', function($table){
          $table->integer('user_id');
        });
    }

    public function down()
    {
        Schema::table('posts',function($table){
          $table->dropColumn('user_id');
        });
    }
}
towhid
towhid
6 days ago (3,030 XP)

hello @Cronix this is my migration db table list

2014_1012000000_create_users_table

2014_10_12_100000_create_password_resets_table

2018_04_15_091102_create_posts_table

Cronix
Cronix
6 days ago (325,610 XP)

If you don't have any real data in the db and can stand to delete it all (all tables), you could try artisan migrate:fresh which will drop all tables, and rerun the migrations from the beginning.

Cronix
Cronix
6 days ago (325,610 XP)

It's strange that your AddUserIdToPosts migration isn't listed in there... Something got messed up. I'd try my above post and migrate from scratch.

towhid
towhid
6 days ago (3,030 XP)

hi @Cronix no i have no real data - it just deploy demo version, btw whats the reason for add this AddUserIdToPosts migration is not listed my migration table ?

Cronix
Cronix
6 days ago (325,610 XP)

I don't know. I'd suggest running artisan migrate:fresh to see if it all works.

towhid
towhid
6 days ago (3,030 XP)

ok my be its run but i have to know if again add any column then its work or not />

bobbybouwmann

Yes it will after that! Just run php artisan:fresh for a clean database and running all migrations or run php artisan:refresh for running all down methods and then all up methods of the migrations ;)

Cronix
Cronix
6 days ago (325,610 XP)

It should. I think just something got messed up. So it migrated everything ok now and the posts table has both user_id and cover_image fields present?

towhid
towhid
6 days ago (3,030 XP)

@Cronix yes - its present , and -- if i try to add new thing also generated new column my exiting post - thank you . but why this problem occurred still i don't understand !

Please sign in or create an account to participate in this conversation.