Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

laravel-4

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.

like image 761
Santo Doni Romadhoni Avatar asked Dec 04 '25 14:12

Santo Doni Romadhoni


1 Answers

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.

like image 140
Santo Doni Romadhoni Avatar answered Dec 07 '25 02:12

Santo Doni Romadhoni