[Laravel] What does Maximum retries of 10000 reached without finding a unique value mean? [Faker]
Hello.
I'm Mandai, in charge of Wild on the development team.
I think you will often use
Faker , which is included It is convenient because it is multilingual and allows you to create a variety of test data, but it also has a method called unique that is useful when creating multiple data with unique IDs, which is also useful. .
However, depending on how you use it, you may get an error like the one in the title, and if you don't know what it is or can't resolve it and have to try another method...then it's a waste to have installed Faker! So, this time, I would like to introduce how to resolve the error "Maximum retries of 10000 reached without finding a unique value" that I often see on Faker.
Cause of error
Judging from the error text, I feel like I'm stuck in an infinite loop somewhere, but most of the time I don't have a concrete idea, and the error occurs around the use of Faker's unique(). Only you can understand.
I haven't written any code that would cause an infinite loop.
If I reset it at the right time, it works fine, but of course, if I reset it, duplication will occur.
There are some things you won't know about the amount of spooning you need until you've used it for a while, but once you know the cause, you'll naturally know how to deal with it...!!
So let's follow the code for Faker's unique() method.
Follow the behavior of unique()
This part of github is the code for the unique() method.
Since we are passing the received variable as is to the UniqueGenerator class, let's further check the code of the UniqueGenerator class.
The behavior is controlled well using magic methods, but the point is that it simply calls the method of the Faker\Generator object that was received when the object was created with UniqueGenerator::__call().
Since the while conditional expression includes a duplicate check, the returned value is unique.
Duplicate checking using array keys seems quite primitive, but the cause is easy to understand.
The title of the error message when OverflowException is thrown indicates that the number of loops in this do while statement has reached the number of retries.
How to avoid errors
There is only one adjustment item to avoid the error, and that is to change the value of the second argument of the unique() method and increase the number of loop retries.
The default is 10,000 times, but if the number of desired values is 15,000, it will always overflow.
To increase the retry count, do the following:
$factory->define(User::class, function(Faker\Generator $faker) { return [ 'name' => $faker->name(), 'age' => $faker->unique(false, 15000) ->numberBetween(1, 80), // Change the retry count to 15000 times ]; });
Another reason is that the Faker\Generator object returns fewer than 10,000 variations.
As in the example above, numberBetween() only returns numbers from 1 to 80, so a unique value cannot be generated for the 81st data, and the loop will be executed more than the number of retries.
I get the impression that there are a lot of variations in random combinations of people's names, etc., but in English there are about 3000 first names for men and women, and 473 last names, so there are a lot of variations, but in Japanese there are only about 50 names for men and women. , there are only 31 surnames and around 1,500 variations of the full name.
Even if you want the full names of 5,000 people in Japanese, if there are only 1,500 combinations, using unique() will be meaningless, so you will need to think of another method.
For example, there seems to be a way to implement and add a new provider to the Faker\Generator class on your own, but I haven't investigated it in detail yet, so I hope to be able to introduce it someday.
summary
This time, I looked into the error "Maximum retries of 10000 reached without finding a unique value" that I often see when using Faker.
It all started when I searched the internet for the error message and found out that StackOverFlow says reset(true) is fine! It all started when I saw something like this and thought it was strange.
I thought there might be some fundamental misunderstanding, so I looked into it, but it seems there was more than one pitfall.
When I came across "Maximum retries of 10000 reached without finding a unique value", I decided to investigate the number of retries and the number of dummy data obtained.
That's it.