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.

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

Successfully merging this pull request may close these issues.

None yet

1 participant