0

I am using Laravel 5.3 and creating some data using Faker

when running locally this code

$factory->define(App\User::class, function (Faker\Generator $faker) {
    $username = str_replace(".","",$faker->username);
    return [
        'username' => $username,
        'email' => $faker->safeEmail,
        'password' => bcrypt('123456'),
        'remember_token' => str_random(10),
    ];
});

produces data where id ranges from 1-50 like in following image http://i.imgur.com/DhIK8JP.png

Another piece of code runs after it and expects user id's to be between 1-50

$factory->define(App\Album::class, function (Faker\Generator $faker) {
    return [
        'hash' => str_random(7),
        'title' => $faker->sentence,
        'user_id' => $faker->numberBetween(1, 50),
        'published' => false,
    ];
});

That all works locally everytime I run php artisan db:seed I am now using Heroku and mysql addon from cleardb.com

when I deploy and run db:seed it produces this every time. Id's allways start from 4 and go up to 494, every time I run it. So when I try to seed on production my db:seed fails since it expects users id's to be 1-50. Any ideas what is going on?

http://i.imgur.com/yZqz70K.png

niko craft
  • 2,893
  • 5
  • 38
  • 67

1 Answers1

0

I think you should reset auto_increment counter on yours table:

ALTER TABLE tablename AUTO_INCREMENT = 1
Vuer
  • 149
  • 6
  • how can I do this in laravel migration file? – niko craft Aug 27 '16 at 16:23
  • You can clear table by: DB::table('table_name')->truncate();. It will reset counter too. – Vuer Aug 27 '16 at 16:24
  • how will truncating table set auto_increment to 1? – niko craft Aug 27 '16 at 16:25
  • I also do it before seed to clear old data. Counter starts with 1 after this. – Vuer Aug 27 '16 at 16:27
  • I don't think truncating will work for me. I've tried it and it didnt do the trick. Here is what I found: ClearDB uses circular replication to provide master-master MySQL support. As such, certain things such as auto_increment keys (or sequences) must be configured in order for one master not to use the same key as the other, in all cases. We do this by configuring MySQL to skip certain keys, and by enforcing MySQL to use a specific offset for each key used. – niko craft Aug 27 '16 at 16:33
  • The reason why we use a value of 10 instead of 2 is for future development. I need to reset values auto_increment_increment and auto_increment_offset to 1, right now on production server they are 10 and 4 which screws everything up. – niko craft Aug 27 '16 at 16:33