Laravel 8 Eloquent migration generated error " 1068 Multiple primary key defined"

49 Views Asked by At

I have the following migration file:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateHeadersTable extends Migration
{
    public function up()
    {
        Schema::create('headers', function (Blueprint $table) {

        $table->increments('entry')->unsigned();
        $table->string('id',16);
        $table->string('address',256);
        $table->string('desciption',512)->nullable()->default('NULL');
        $table->tinyInteger('currency',)->default('0');
        $table->decimal('sum',13,4);
        $table->datetime('entered');
        $table->tinyInteger('aborted',)->default('0');
        $table->primary('entry');

        });
    }

    public function down()
    {
        Schema::dropIfExists('headers');
    }
}

This file was automatically generated by an online tool from a SQL file. However, when I ran php artisan migrate, I got the following error:

  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'headers'
already exists (SQL: create table `headers` (`entry` int unsigned not null
auto_increment primary key, `id` varchar(16) not null, `address`
varchar(256) not null, `desciption` varchar(512) null default 'NULL', 
`currency` tinyint not null default '0', `sum` decimal(13, 4) not null, 
`entered` datetime not null, `aborted` tinyint not null default '0') 
default character set utf8mb4 collate 'utf8mb4_unicode_ci')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:712
    708▕         // If an exception occurs when attempting to run a query, we'll format the error
    709▕         // message to include the bindings with SQL, which will make this exception a
    710▕         // lot more helpful to the developer instead of just the database's errors.
    711▕         catch (Exception $e) {
  ➜ 712▕             throw new QueryException(
    713▕                 $query, $this->prepareBindings($bindings), $e
    714▕             );
    715▕         }
    716▕     }

      +9 vendor frames
  10  database/migrations/2023_01_31_1675138133_create_headers_table.php:23
      Illuminate\Support\Facades\Facade::__callStatic()

      +22 vendor frames
  33  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

I am not familiar with Laravel migration files. How can I fix this? Many thanks!

2

There are 2 best solutions below

0
simonecosci On BEST ANSWER

Do not drop the table manually. use php artisan migrate:rollback and then re-try php artisan migrate

0
Sagar Sheregar On

Error you got states that Table 'headers' already exists in database if you have deleted table from database just check their may be entry in migrations table in database just delete it and run migration again