Santo Doni Romadhoni
Santo Doni Romadhoni

Reputation: 81

How to create multiple primary key with auto_increment include in laravel migrations?

I've just new with Laravel. I have a problem when doing migrations. My Schema is just like this

public function up()
{
    Schema::create('journal', function($table){
        $table->increments('id');
        $table->timestamp('journal_date');
        $table->string('no_ref',25);
        $table->string('acc_id', 10);
        $table->string('description', 100);
        $table->integer('debet')->default(0);
        $table->integer('kredit')->default(0);
        $table->primary(array('journal_date', 'no_ref', 'acc_id'));
    });
}

Then when running PHP artisan migrate I get an an error

[Illuminate\Database\QueryException]                                                                                                                                                            
SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key 
defined (SQL: alter table `journal` add primary key 
journal_journal_date_no_ref_acc_id_primary(`journal_date`,   
`no_ref`, `acc_id`))    

I did some advice to drop primary but this will drop auto increment too. I just don't know how to figure it out.

Upvotes: 3

Views: 4432

Answers (3)

Alleks
Alleks

Reputation: 11

The auto-increment field must be a key (for example, MySQL will not even allow you to define an auto_increment column if it is not a key). That's probably why you're not able to simply drop the key.Define a second key for the field before dropping. ($table->increments('id')->unique();).

    public function up() {
    Schema::create('journal', function($table) {
        $table->increments('id')->unique();
        $table->timestamp('journal_date');
        $table->string('no_ref', 25);
        $table->string('acc_id', 10);
        $table->string('description', 100);
        $table->integer('debet')->default(0);
        $table->integer('kredit')->default(0);

    });
    Schema::table('journal', function($table) {
        $table->dropPrimary('id');
        $table->primary(['journal_date', 'no_ref', 'acc_id']);
    });
}

Upvotes: 1

I found this solution, Please follow up. The small changes in create the primary key.

public function up()
{
//
    Schema::create('journal', function($table){
        $table->unsignedInteger('id');
        $table->timestamp('journal_date');
        $table->string('no_ref',25);
        $table->string('acc_id', 10);
        $table->string('description', 100);
        $table->integer('debet')->default(0);
        $table->integer('kredit')->default(0);
        $table->primary(array('id', 'journal_date', 'no_ref', 'acc_id'));
    });
}

Upvotes: 0

Santo Doni Romadhoni
Santo Doni Romadhoni

Reputation: 81

Finally, I found the answer. I've just used DB::statement like this

                DB::statement('ALTER TABLE  `journal` DROP PRIMARY KEY , ADD PRIMARY KEY (  `id` ,  `journal_date` ,  `no_ref` ,  `acc_id` ) ;');

And my problem solved.

Upvotes: 3

Related Questions