SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name ‘id’

I got an error after trying to migrate.
And I didn’t make any changes in other pages.

Every tables created as migrate default but it did not create my articles table.

This is the error that I got in CMD:
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name ‘id’

And this is my _create_articles_table.php page:

<?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

class CreateArticlesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->id();
            $table->increments('id');
            $table->timestamps();
            $table->string('title');
            $table->text('body');
            $table->boolean('status');

        });
    }

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

and this is my AppServiceProvider page:
<?php

namespace AppProviders;

use IlluminateSupportServiceProvider;
use Schema;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        
    }

}

Thank you for your time.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Clearly, you are defining id column twice, no need to add this line:

$table->increments('id');

This will make it with auto-increment by default:
$table->id();

Method 2

CMD: SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name ‘id ‘

That means your id field is defined twice in the migration query so remove this line in your articles.

$table->increments('id');

Then migrate your table it is run.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x