Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pkouadio's avatar

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

Hi! I'm trying to run my database migration and I get this error

SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'id_jeciste' (SQL: create table militer (id int unsigned not null auto_increment primary key, id_jeciste bigint unsigned not null, id_annee bigint unsigned not null, level_instance int unsigned not null, instance int unsigned not null, poste varchar(255) not null, debut date null, fin date null, created_at timestamp null, updated_at timestamp null, id_jeciste bigint unsigned not null, id_annee bigint unsigned not null ) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

Here is the code of the migration of the concerned table

class CreateJecMiliterTable extends Migration  {  
/**  
* Run the migrations. *
* * @return void  */  
* public function up()  
{  
    Schema::disableForeignKeyConstraints();  
    Schema::create('militer', function (Blueprint $table) {  
        $table->increments('id');
        $table->unsignedBigInteger('id_jeciste');
        $table->unsignedBigInteger('id_annee'); 
        $table->unsignedInteger('level_instance');
        $table->unsignedInteger('instance');
        $table->string('poste');
        $table->date('debut') -> nullable();
        $table->date('fin') -> nullable();
        $table->timestamps();
        
        $table -> foreignId('id_jeciste')  
                -> references('id')->on('jecistes')  
                ->onDelete('cascade')->onUpdate('cascade');
                
        $table -> foreignId('id_annee')  
                -> references('id')->on('annees_pastorales')  
                ->onDelete('cascade')->onUpdate('cascade');
                });
    } 
    /**
     * Reverse the migrations. *
     * * @return void */
     
    public function down(){  
    Schema::table('militer', function (Blueprint $table) {  
        $table->dropForeign(['id_jeciste']);
        $table->dropForeign(['id_annee']);
        });
        Schema::dropIfExists('militer');
        } 
    }

I tried to do php artisan migrate:refresh but I have the same issue

0 likes
4 replies
psrz's avatar

You are indeed trying to create the same field twice

       $table->unsignedBigInteger('id_jeciste');
        ....
        $table -> foreignId('id_jeciste')  
                -> references('id')->on('jecistes')  
                ->onDelete('cascade')->onUpdate('cascade');

Remove one of them.

1 like
OussamaMater's avatar
Level 37

You either use foreignId with constrained or foreign with unsignedBigInteger.

In your case, you are using foreignId with unsignedBigInteger

$table->unsignedBigInteger('id_jeciste');
$table->foreignId('id_jeciste');

So the solution is just rename foreignId to foreign, so your code is like this

$table->unsignedBigInteger('id_jeciste');
$table->foreign('id_jeciste')  
       ->references('id')
	   ->on('jecistes')  
       ->onDelete('cascade')
	   ->onUpdate('cascade');

Please or to participate in this conversation.