Geoffrey
Geoffrey

Reputation: 467

Migration: Column already exists error when creating new tables

I did a search on possible resolutions to this issue and I couldn't find anything when creating a new table in Laravel 7. But, I am getting a Column already exists error when running php artisan migrate while creating new tables. The column that is in the error is contract_type_id.

Here is a snip of the migration file:

public function up()
{
    Schema::create('contract_data', function (Blueprint $table) {
        $table->id();
        $table->integer('contract_type_id');
        $table->integer('customer_id');
        $table->date('start_date');
        $table->date('end_date');
        $table->integer('product1_id');
        $table->integer('product2_id');
        $table->integer('product3_id');
        $table->integer('product4_id');
        $table->integer('product5_id');
        $table->integer('product1_gallons');
        $table->integer('product2_gallons');
        $table->integer('product3_gallons');
        $table->integer('product4_gallons');
        $table->integer('product5_gallons');
        $table->decimal('product1_price', 9,6 );
        $table->decimal('product2_price', 9,6 );
        $table->decimal('product3_price', 9,6 );
        $table->decimal('product4_price', 9,6 );
        $table->decimal('product5_price', 9,6 );
        $table->integer('fuel_contract_number');
        $table->timestamps();
    });

    Schema::table('contract_data', function(Blueprint $table) {
        $table->foreignId('contract_type_id')->constrained('contract_type');
        $table->foreignId('customer_id')->constrained('customer_details');
        $table->foreignId('product1_id')->constrained('product_details');
        $table->foreignId('product2_id')->constrained('product_details');
        $table->foreignId('product3_id')->constrained('product_details');
        $table->foreignId('product4_id')->constrained('product_details');
        $table->foreignId('product5_id')->constrained('product_details');
        $table->foreignId('fuel_contract_number')->constrained('fuel_contracts');
    });
}

Upvotes: 0

Views: 1332

Answers (1)

STA
STA

Reputation: 34678

The foreignId method is an alias for unsignedBigInteger while the constrained method will use convention to determine the table and column name being referenced
So you are making contract_type_id field twice.

$table->foreignId('contract_type_id')->constrained('contract_type'); alias of $table->unsignedBigInteger('contract_type_id'); & $table->foreign('contract_type_id')->references('id')->on('contract_type);

So in your second migration it would be :

$table->foreign('contract_type_id')->references('id')->on('contract_type);

Instead of :

$table->foreignId('contract_type_id')->constrained('contract_type');

Also you need to use Unsigned Big Integer, as :

$table->unsignedBigInteger('contract_type_id');

Not,

$table->integer('contract_type_id');

Because Laravel 5.8 Added unsignedBigInteger as defaults for foreign key, also for Laravel 6 and 7

Upvotes: 3

Related Questions