Fateme rmz
Fateme rmz

Reputation: 37

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 Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

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 App\Providers;

use Illuminate\Support\ServiceProvider;
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.

Upvotes: 0

Views: 9741

Answers (2)

Switi
Switi

Reputation: 379

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.

Upvotes: 1

ikoujar
ikoujar

Reputation: 264

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();

Upvotes: 4

Related Questions