3

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.

2
  • similar post: stackoverflow.com/questions/23098207/… Commented May 29, 2014 at 1:50
  • No no no, its different. This case is not about foreign key but multiple primary key, which is include auto_increment Commented May 29, 2014 at 9:14

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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']);
    });
}

1 Comment

Welcome to StackOverflow. Can you please provide a description on how your code works? That way people who stumble across your answer understand what it is doing and how it is doing it? Please read this article to get a better understand of what an answer is. Please also go through the site tour
0

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'));
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.