-
Notifications
You must be signed in to change notification settings - Fork 328
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
Feature: Unique key #848
base: 1.23
Are you sure you want to change the base?
Feature: Unique key #848
Conversation
For now, I have created a small package that adds a |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 1 week if no further activity occurs. Thank you for your contributions. |
"Activity" 🤖 |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 1 week if no further activity occurs. Thank you for your contributions. |
* | ||
* @throws \OverflowException When no unique value can be found by iterating $maxRetries times | ||
* | ||
* @return self A proxy class returning only non-existing values | ||
*/ | ||
public function unique($reset = false, $maxRetries = 10000) | ||
public function unique($reset = false, $maxRetries = 10000, $key = 'default') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could also pass &$uniques = []
to init generator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I can't follow.
I've tried this Provider $this->app->singleton('faker.unique.users.email', function ($app) {
$uniques = User::pluck('email')->all();
return new UniqueGenerator(fake(), 10000, $uniques);
}); Factory 'email' => app('faker.unique.users.email')->safeEmail(), Still not enough for paratest to fully avoid database exceptions for unique keys |
What is the reason for this PR?
Author's checklist
Summary of changes
When using FakerPHP in database factories, such as in Laravel, I frequently encounter the need for unique values for a specific model or column. For example, unique emails for that model or a non-auto-incrementing ID that needs to be set.
Consider this implementation of a Laravel database factory:
Because of the
->unique()
method, we obtain a uniqueexternal_id
for each user. However, we may have another model that also requires unique numbers. Ideally, the IDs should be reset for this model. However, calling->unique(reset: true)
would reset the entire generator, causing it to reset on every call.Therefore, I propose an option to add a key so that people can have multiple unique generators. This would allow us to have an independent set of unique numbers for both factories and models.
What do you think about this?
I'm not sure whether you consider
$uniqueGenerator
internal or if removing$uniqueGenerator
and adding$uniqueGenerators
is considered a breaking change.Maybe this should be implemented as a new method, but I couldn't think of a better name than
keyedUnique()
. It appears that there is some confusion surrounding the usage of->unique()
in general.laravel/framework#46287
laravel/laravel#5137
Review checklist
CHANGELOG.md