You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Panic on overlapping one-to-one relationships (#18833)
# Objective
One to one relationships (added in
#18087) can currently easily be
invalidated by having two entities relate to the same target.
Alternative to #18817 (removing one-to-one relationships)
## Solution
Panic if a RelationshipTarget is already targeted. Thanks @urben1680 for
the idea!
---------
Co-authored-by: François Mockers <[email protected]>
@@ -339,6 +339,12 @@ impl RelationshipSourceCollection for Entity {
339
339
}
340
340
341
341
fnadd(&mutself,entity:Entity) -> bool{
342
+
assert_eq!(
343
+
*self,
344
+
Entity::PLACEHOLDER,
345
+
"Entity {entity} attempted to target an entity with a one-to-one relationship, but it is already targeted by {}. You must remove the original relationship first.",
346
+
*self
347
+
);
342
348
*self = entity;
343
349
344
350
true
@@ -355,7 +361,11 @@ impl RelationshipSourceCollection for Entity {
355
361
}
356
362
357
363
fniter(&self) -> Self::SourceIter<'_>{
358
-
core::iter::once(*self)
364
+
if*self == Entity::PLACEHOLDER{
365
+
None.into_iter()
366
+
}else{
367
+
Some(*self).into_iter()
368
+
}
359
369
}
360
370
361
371
fnlen(&self) -> usize{
@@ -372,7 +382,13 @@ impl RelationshipSourceCollection for Entity {
"Entity {entity} attempted to target an entity with a one-to-one relationship, but it is already targeted by {}. You must remove the original relationship first.",
0 commit comments