-
Notifications
You must be signed in to change notification settings - Fork 10.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Faker unique() not working properly #46287
Comments
@binaryfire the unique aspect of faker is only during a single invocation of the Framework. For example:
Within each of these commands Faker should generate unique values, however it does not generate unique values across them. So unique values generated in Invocation 1 may also be generated in Invocation 2 and 3. Each time PHP shuts down Faker will reset its unique cache and may then generate the same values again. Within a single invocation: Across multiple invocations: Hope this helps, but let us know if we have missed something. |
@timacdonald Hey Tim. Yeah I’m aware of that. The problem occurs on a single invocation. What I meant by “rinse and repeat” is to “nuke the db and try again” if you can't reproduce the problem the first time. Hitting a dupe isn’t guaranteed even if unique() is broken- it’s a matter of probability. Did you try seeding hundreds of records at once and attempt to reproduce the problem several times, starting with an empty db each time? Here's some dummy files to use: Factory:
Seeder:
Example of the problem using the above 2 files: |
@timacdonald Have done a bit more investigation. I'm using From the Faker docs:
But using that for a single generator breaks exception handling for all the other generators. Using
Is there anything that can be done to improve this? I can generate emails and domains using |
@timacdonald Calling faker directly works 100% of the time. This will work:
|
The This is the expected behaviour. If you need different functionality, I would recommend accessing an instance of Faker directory and not use the |
@binaryfire I think the reset flag shouldn't be used inside factories ( |
Description:
fake()->unique()
doesn't seem to be working properly, at least for the generators I've tried. Some examples:numberBetween
domainName
domainWord
word
Some variations I've tried that have created dupes:
Example error msg:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'pollich.biz' for key.........
I'm only creating 200-300 records so
numberBetween
shouldn't be running out of unique values. But according to the Faker docs an exception should be thrown if a duplicate value is generated: https://fakerphp.github.io/#modifiersA few others are having the same issue too: https://laracasts.com/discuss/channels/laravel/faker-unique-not-always-working
To reproduce:
The text was updated successfully, but these errors were encountered: