Commit 069f110
Rework one-to-one relationships to remove existing instead of panicking (#20232)
# Objective
- Issue: #18847
- Resolves panic when establishing a one-to-one relationship to an
entity that already has an existing relationship from another entity.
## Solution
- Modified the `on_insert` hook in the `Relationship` trait to detect
one-to-one relationships (where the target collection type is `Entity`)
and automatically remove any existing relationship before establishing
the new one.
- Uses runtime type checking with `TypeId` to identify one-to-one vs
one-to-many relationships.
- Safely removes the existing source relationship using `try_remove()`
to handle edge cases gracefully.
## Testing
- Removed panic assertions from `Entity::add()` and
`Entity::extend_from_iter()` methods that would previously crash when
attempting to establish overlapping one-to-one relationships
- Modified existing test `one_to_one_relationship_shared_target` by
removing `#[should_panic]` and adding assertions to verify:
- Original relationship is properly removed
- New relationship is correctly established
- Target entity points to the new source
---------
Co-authored-by: Chris Russell <8494645+chescock@users.noreply.github.com>1 parent 5bb8f29 commit 069f110
File tree
2 files changed
+45
-14
lines changed- crates/bevy_ecs/src/relationship
2 files changed
+45
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
131 | 146 | | |
132 | 147 | | |
133 | 148 | | |
| |||
Lines changed: 30 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
72 | 78 | | |
73 | 79 | | |
74 | 80 | | |
| |||
345 | 351 | | |
346 | 352 | | |
347 | 353 | | |
348 | | - | |
349 | | - | |
350 | | - | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | 354 | | |
355 | | - | |
356 | 355 | | |
357 | 356 | | |
358 | 357 | | |
| |||
389 | 388 | | |
390 | 389 | | |
391 | 390 | | |
392 | | - | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
397 | | - | |
398 | 391 | | |
399 | 392 | | |
400 | 393 | | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
401 | 402 | | |
402 | 403 | | |
403 | 404 | | |
| |||
724 | 725 | | |
725 | 726 | | |
726 | 727 | | |
727 | | - | |
728 | 728 | | |
729 | 729 | | |
730 | 730 | | |
| |||
740 | 740 | | |
741 | 741 | | |
742 | 742 | | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
743 | 759 | | |
744 | 760 | | |
745 | 761 | | |
| |||
0 commit comments