Asmaa outaleb
Asmaa outaleb

Reputation: 1

Laravel 8 problem during migration creation

I have an error when I run my migration.

SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'binom_id' (SQL: alter table profs add binom_id int unsigned null, add note_id int unsigned null)

Migration

class CreateProfsTable extends Migration
{
    public function up()
    {
        Schema::dropIfExists('profs');

        Schema::create('profs', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('nom')->nullable();
            $table->string('email')->nullable();
            $table->string('password')->nullable();
            $table->unsignedBigInteger('binom_id');
            $table->unsignedBigInteger('note_id');
            $table->timestamps();
        });
    }
    
    public function down()
    {
        Schema::dropIfExists('profs');
    }
}

Migration related to the foreign key.

class Pprof extends Migration
{
    public function up()
    {
        Schema::table('profs', function (Blueprint $table) {
             $table->integer('binom_id')->unsigned()->nullable();
            $table->foreign('binom_id')->references('id')->on('binomes')->onUpdate('cascade')->onDelete('cascade');
            $table->integer('note_id')->unsigned()->nullable();         
            $table->foreign('note_id')->references('id')->on('notes')->onUpdate('cascade')->onDelete('cascade');
            Schema::enableForeignKeyConstraints();
        });
    }

    public function down()
    {   
        Schema::table('profs', function (Blueprint $table) {
            Schema::dropColumn('binom_id');
            $table->dropForeign('binom_id');
            Schema::dropColumn('note_id');
            $table->dropForeign('note_id');
       Schema::disableForeignKeyConstraints();
        });
    }
}

Upvotes: 0

Views: 55

Answers (1)

Peppermintology
Peppermintology

Reputation: 10220

Your Pprof migration is trying to add another column with the name binom_id to the profs table when it has already been added in your CreateProfsTable migration. Removing that line will solve the error.

So in Pprof migration, remove:

$table->integer('binom_id')->unsigned()->nullable();

You will also get an error for your note_id column as that is defined twice too, so the same applies, remove the following from the Pprof migration:

 $table->integer('note_id')->unsigned()->nullable();   

Alternatively, just combine your two migrations:

public function up()
{
    Schema::dropIfExists('profs');

    Schema::create('profs', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('nom')->nullable();
        $table->string('email')->nullable();
        $table->string('password')->nullable();
        $table->unsignedBigInteger('binom_id')->nullable();
        $table->unsignedBigInteger('note_id')->nullable();
        $table->timestamps();
    
        $table->foreign('binom_id')->references('id')->on('binomes')->onUpdate('cascade')->onDelete('cascade');
        $table->foreign('note_id')->references('id')->on('notes')->onUpdate('cascade')->onDelete('cascade');
    });
}

Upvotes: 1

Related Questions