Search code examples
phpdatabaselaravel-5migrationmysql-error-1068

php artisan migrate. Multiple primary key defined


My problem

I'm building my first api using Laravel and am getting a multiple primary key defined error while trying to run php artisan migrate I don't understand how I have defined multiple primary keys. Every time I run a migrate I get these errors php artisan migrate error.

My Troubleshooting

I thought since autoIncrementing() can only be used for a primary key that maybe that defined it as a primary key, so I altered $table->increments('bus_id')->autoIncrement()->primary(); to $table->increments('bus_id')->autoIncrement(); and $table->increments('bus_id')->autoIncrement();

Every time I had tried to run my migrations, I dropped my database and re-created it and tried to run my migrations again (so it was a new database every time with no corrupt data) but still didn't make a difference.

I checked my Connection.php that was mentioned in the picture of my error code above but didn't see anything pertaining to any primary keys.

My Question

My code is below, can someone help me understand how I'm making double primary keys?

    <?php

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

class CreatePurchaseHistoryTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('purchase_history', function (Blueprint $table) {
            $table->increments('pur_id', 11);
            $table->string('pur_item', 255);
            $table->dateTimeTz('pur_date');
            $table->string('bus_name', 50);
            $table->double('pur_amount', 10, 2);
            $table->double('cashback_amount', 10, 2);
            $table->integer('bus_id', 11);
            $table->foreign('bus_id')->references('bus_id')->on('business');   
            $table->timestamps();
            $table->rememberToken();  
            $table->engine = 'InnoDB';  
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('purchase_history');
    }
}

Please note there are similar questions here on stack overflow which I have also tried their solutions without any success After edit I'm still getting this error:

In Connection.php line 664:
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto colu mn and it must be defined as a key (SQL: create table purchase_history (pur_id int unsigned not null auto_incre ment primary key, pur_item varchar(255) not null, pur_date datetime not null, bus_name varchar(50) not null, pur_amount double(10, 2) not null, cashback_amount double(10, 2) not null, bus_id int not null auto_increment primary key, created_at timestamp null, updated_at timestamp null, remember_token varchar(100) null) default character set utf8mb4 collate utf8mb4_unicode_ci engine = InnoDB)

After running php artisan migrate:refresh migrate:refresh error

business table


Solution

  • Try these

    1. Remove value on increments
    2. If bus_id is and foreign key then unsigned it before add references
    3. Important: Make sure category has defined/created before purchase_history table and category table auto-increment field is bus_id

    $table->increments('pur_id'); 
    $table->integer('bus_id')->unsigned();
    $table->foreign('bus_id')->references('bus_id')->on('category'); 
                                           ^ make sure category table auto increment field value is bus_id
    

    FYI: If I draw table the I set id as a name on the auto-increment field. because no confusion.