|
|
|
@ -507,11 +507,15 @@ impl Storage { |
|
|
|
|
let mut relations_to_insert = vec![]; |
|
|
|
|
let mut relations_to_delete = vec![]; |
|
|
|
|
|
|
|
|
|
let existing_relations_by_id = self.relations.values() |
|
|
|
|
let mut existing_relations_by_id = self.relations.values() |
|
|
|
|
.filter(|v| v.object == updated_object_id) |
|
|
|
|
.into_group_map_by(|v| v.model); |
|
|
|
|
|
|
|
|
|
for (relation_model_id, relation_model) in self.rel_models.iter().filter(|(_id, r)| r.object == updated_object_model_id) { |
|
|
|
|
let rel_models_by_id = self.rel_models.iter().filter(|(_id, r)| r.object == updated_object_model_id); |
|
|
|
|
|
|
|
|
|
for (relation_model_id, relation_model) in rel_models_by_id { |
|
|
|
|
let mut updated_ids = vec![]; |
|
|
|
|
|
|
|
|
|
if let Some(instances) = relations_by_model.remove(relation_model_id) { |
|
|
|
|
if instances.len() > 1 && !relation_model.multiple { |
|
|
|
|
return Err(StorageError::ConstraintViolation(format!("{} of {} cannot be set multiply", relation_model, obj_model).into())); |
|
|
|
@ -541,12 +545,17 @@ impl Storage { |
|
|
|
|
model: rel_instance.model, |
|
|
|
|
related: rel_instance.related, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
updated_ids.push(relation_id); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if !relation_model.optional { |
|
|
|
|
return Err(StorageError::ConstraintViolation(format!("{} is required for {}", relation_model, obj_model).into())); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
relations_to_delete.extend(existing_relations_by_id.remove(&relation_model_id).unwrap_or_default().into_iter() |
|
|
|
|
.filter(|rel| !updated_ids.contains(&rel.id)).map(|rel| rel.id)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let obj_mut = self.objects.get_mut(&updated_object_id).unwrap(); |
|
|
|
|