Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nikocraft's avatar

Runnin Faker locally and on production creates different set of Id's

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

0 likes
5 replies
ohffs's avatar

The table probably has an AUTO_INCREMENT=4 (or approx - too lazy to do the arithmetic) on the production site. If you can get to some kind of mysql console on heroku then you can try a show create table name-of-table and see how it's defined.

nikocraft's avatar

I found out that: "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. The reason why we use a value of 10 instead of 2 is for future development."

and I need to do this in migration:

    DB::update("ALTER TABLE users AUTO_INCREMENT = 1;");
    DB::update("ALTER TABLE users AUTO_INCREMENT_OFFSET = 1;");
gator's avatar

Also, Autoincrementing ID's only purpose is to serve as Primary key.. i.e. to make rows unique in the table They should not mean anything to you, your app or your tests. So, rewrite your test to count the number of rows and expect 50 - the increment, begin or end should not be of importance.

nikocraft's avatar

rewritting this I dont see how I can do it: $faker->numberBetween(1, 50) if id's range from 4-494, it's a guess game which id's are taken or not. Also doing DB::update("ALTER TABLE users AUTO_INCREMENT = 1;"); DB::update("ALTER TABLE users AUTO_INCREMENT_OFFSET = 1;");

did not work. For some reason ClearDB resets them again to 10 and 4 so my db:seed fails again.

shez1983's avatar

You can just do a normal select ids from table and then pick a random id from that..

I gave u pure sql. U wud of course do eloquent id needed in ur other seeder that relies on the ids from first table

Please or to participate in this conversation.