Skip to content

Conversation

@spawnia
Copy link
Collaborator

@spawnia spawnia commented Feb 4, 2026

Summary

Fixes two issues with nested HasOne mutations when a related record already exists:

  • upsert without ID: Now correctly updates the existing record instead of creating a duplicate
  • create: Now throws a clear error instead of silently creating a duplicate record

Changes

upsert fix

When using upsert on a HasOne relation without providing an id, the existing record is now found and updated rather than creating a new one.

create validation

When using create on a HasOne relation where a related record already exists, a descriptive error is thrown:

Cannot create a related model: a Post already exists for this Task. Use upsert to modify the existing model.

Test plan

  • Added test testCreateHasOneWhenAlreadyExists verifying error is thrown
  • Existing test testUpdateAndUpsertExistingHasOneWithoutID covers upsert fix
  • All 18 HasOneTest cases pass

Related

🤖 Generated with Claude Code

spawnia and others added 6 commits February 4, 2026 13:13
When using `upsert` on a HasOne relation without providing an ID, the
existing implementation always created a fresh model via `$relation->make()`.
This caused duplicate records to be created instead of updating the
existing related record.

Now checks if a record already exists via `$relation->first()` before
falling back to creating a new model instance.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@spawnia spawnia added the bug An error within Lighthouse label Feb 4, 2026
spawnia and others added 3 commits February 5, 2026 09:20
Previously, using `create` on a hasOne relation when a related model
already exists would silently create a second record, corrupting the
relationship. Now throws a GraphQL error guiding users to use `upsert`.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@spawnia spawnia changed the title Fix upsert for HasOne when existing record has no ID in input Fix nested HasOne mutations when related record exists Feb 5, 2026
@spawnia spawnia merged commit 03515fa into master Feb 5, 2026
65 checks passed
@spawnia spawnia deleted the fix-has-one-upsert-existing branch February 5, 2026 08:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug An error within Lighthouse

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant