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
$table->increments(...)
creates an auto-incrementing primary key field. So->autoIncrement()->primary()
are both redundant, and probably causing the error. – James$table->increments('bus_id');
– The Alpha