Skip to content

Fix replacing one-sided relationships after target removal - #847

Merged
hrach merged 2 commits into
mainfrom
js/fix-one-sided-replacement
Nov 8, 2026
Merged

hrach merged 2 commits into
mainfrom
js/fix-one-sided-replacement

Conversation

@hrach

@hrach hrach commented Nov 6, 2026

Copy link
Copy Markdown
Member

Replacing a lazy one-sided relationship after its target was removed tried to
load the deleted entity and threw NoResultException. Only load and track the
previous target when a reverse relationship needs updating, allowing replacement
or null assignment without resolving the deleted target.

Add 16 regression cases for one-sided 1:1 and m:1 relationships with loaded and
lazy references, entity and ID assignments, new targets, and null. Verify the
relationship after reloading and ensure the deleted target stays deleted.

Validation: all 328 tests passed using PostgreSQL; PHPStan and DressCode checks
for the changed files passed.

Fixes #846

@hrach

hrach commented Nov 8, 2026

Copy link
Copy Markdown
Member Author

This change is part of the following stack:

Change managed by git-spice.

@hrach
hrach force-pushed the js/fix-one-sided-replacement branch from d4bb855 to c2e63ea Compare November 8, 2026 19:27
hrach added 2 commits November 8, 2026 21:03
Avoid resolving the previous target when replacing an unloaded one-sided
relationship. This allows assigning a replacement or null after the previous
target was removed, without throwing NoResultException. Preserve tracking of
already loaded targets and updates to reverse relationships.

Add database regression tests using the existing Book::nextPart relationship
as one-sided through test-local metadata. Clear the stored reference and
delete its target through DBAL, leaving the loaded owner's lazy reference
stale while preserving foreign-key constraints. Verify entity, ID,
new-entity and null assignments through persistence and reload, including
an already loaded reference.

Fixes #846
Append the current HasOne entity to the tracked entities instead of combining
the arrays with a union. Both arrays can contain key 0, causing a tracked
previous target to hide the replacement and skip its pending changes.

Cover bidirectional 1:1 and m:1 relationships using the existing fixtures.
Verify that both previous and replacement targets are queued and their
changes survive persistence and reload.
@hrach
hrach force-pushed the js/fix-one-sided-replacement branch from c2e63ea to 036ada4 Compare November 8, 2026 20:09
@hrach
hrach merged commit e1b9035 into main Nov 8, 2026
15 checks passed
@hrach
hrach deleted the js/fix-one-sided-replacement branch November 8, 2026 20:13
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.

Replacing a lazy one-sided relationship after removing its target throws NoResultException

1 participant