Skip to content

Individual factory creates two Individual records bound to the same user #2596

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

Open
1 task done
jobara opened this issue Mar 13, 2025 · 0 comments
Open
1 task done
Labels
bug Something isn't working

Comments

@jobara
Copy link
Collaborator

jobara commented Mar 13, 2025

Prerequisites

Describe the bug

When using the Individual Factory it actually creates two Individual records, both of which are bound to the same user. This is because there is a UserObserver that creates a new individual record when a user is created with the individual context. The user only supports a hasOne relationship to the Individual. This means that one of the Individual records won't be accessible through it. Which causes issues when the Individual Factory is used in tests, which we tend to have to work around by getting the Individual through the user.

To Reproduce

Steps to reproduce the behavior:

  1. use tinker php artisan tinker
  2. run Individual::factory()->create();
  3. look in the database and notice that there are two Individual records created both pointing at the same user id

Expected behavior

Using the Individual factory should only create the requested number of Individual records, all tied to a unique user record.

Screenshots

No response

Details

  • Device:
  • Operating system:
  • Browser:
  • Link to affected page:

Additional context

Options to consider for fixing:

  • The UserObserver shouldn't create a new Individual record if the user account is already bound to an Individual record
    • The user account may be created before the Individual from the factory. If that's the case, this will be too late to check if a second Individual will be created.
  • The individual factory shouldn't create an Individual directly, but rather defer to the one created via a user with the individual context. Then update the individual data with the values passed into the factory, and return the updated individual.
@jobara jobara added the bug Something isn't working label Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant