Skip to content
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

Open
wants to merge 2 commits into
base: 1.23
Choose a base branch
from
Open

Conversation

pxlrbt
Copy link

@pxlrbt pxlrbt commented Jan 12, 2024

What is the reason for this PR?

  • A new feature
  • Fixed an issue (resolve #ID)

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:

class UserFactory extends Factory
{
    public function definition(): array
    {
        return [
            'external_id' => $this->faker->unique()->numberBetween(0, 99),
        ];
    }
}

Because of the ->unique() method, we obtain a unique external_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.

class EmployeeFactory extends Factory
{
    public function definition(): array
    {
        return [
            'external_id' => $this->faker->unique(reset: true)->numberBetween(0, 99),
        ];
    }
}

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.

class UserFactory extends Factory
{
    public function definition(): array
    {
        return [
            'external_id' => $this->faker->unique(key: 'user.external_id')->numberBetween(0, 99),
        ];
    }
}

class EmployeeFactory extends Factory
{
    public function definition(): array
    {
        return [
            'external_id' => $this->faker->unique(key: 'employee.external_id')->numberBetween(0, 99),
        ];
    }
}

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

  • All checks have passed
  • Changes are added to the CHANGELOG.md
  • Changes are approved by maintainer

@pxlrbt pxlrbt changed the title Unique key Feature: Unique key Jan 12, 2024
@pxlrbt
Copy link
Author

pxlrbt commented Jan 12, 2024

For now, I have created a small package that adds a betterUnique() method that allows a key as first param:
https://github.com/pxlrbt/faker-better-unique

Copy link

stale bot commented Mar 13, 2024

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.

@pxlrbt
Copy link
Author

pxlrbt commented Mar 13, 2024

"Activity" 🤖

@stale stale bot removed the lifecycle/stale label Mar 13, 2024
Copy link

stale bot commented Apr 22, 2024

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')
Copy link

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

Copy link
Author

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.

@ulcuber
Copy link

ulcuber commented Jun 15, 2024

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
But at least it could help to split by keys to prevent OverflowException

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants