0

I created a seeder for a table, and the first record I am trying to insert has ID=0. The ID is an auto increment field, and when it gets inserted, I check its ID and it is 1 instead of 0, so the seeder breaks in the next line, when I try to create a record with ID 1.

How can I insert several records, with the first having an ID of 0?

This is the code I have:

DB::table('payment_status')->delete();
DB::table('payment_status')->insert(array('id' => '0', 'name' => 'Initial'));
DB::table('payment_status')->insert(array('id' => '1', 'name' => 'Payment successful, Waiting for Processing'));
Miguel Mesquita Alfaiate
  • 2,851
  • 5
  • 30
  • 56
  • (1) Why wouldn't you let auto-increment do it's job? (2) You should be able to UPDATE the first record to 0. (3) From past experience, I would avoid 0 in auto-increment fields; MySQL maintenance routines see a value of 0 in an auto-increment field as something that needs "fixed". – Uueerdo Jun 16 '16 at 16:21
  • http://stackoverflow.com/questions/27000239/how-to-set-auto-increment-in-laravel-with-eloquent – Hedegare Jun 16 '16 at 16:21
  • @Jackowski that suggestion does not seem to work. I tried running that alter table option with the value as zero and the same error happens – Miguel Mesquita Alfaiate Jun 16 '16 at 16:35
  • @Uueerdo thank you for the suggestion, I updated the row afterwards! – Miguel Mesquita Alfaiate Jun 16 '16 at 16:37

1 Answers1

1

I have been able to get this working thanks to the suggestion of Uueerdo, updating the row after it has been inserted:

DB::table('payment_status')->delete();
DB::table('payment_status')->insert(array('id' => '0', 'name' => 'Initial'));
// id zero does not work
DB::unprepared("UPDATE payment_status SET id=0");
DB::table('payment_status')->insert(array('id' => '1', 'name' => 'Payment successful, Waiting for Processing'));
Miguel Mesquita Alfaiate
  • 2,851
  • 5
  • 30
  • 56