diff --git a/src/linkahead/common/models.py b/src/linkahead/common/models.py index 0b66acd99037e1122df66c4476181730e5a4aed4..35db9f62faf0db773d7547f119d43b5710803fea 100644 --- a/src/linkahead/common/models.py +++ b/src/linkahead/common/models.py @@ -751,8 +751,17 @@ class Entity: # If the name is a special attribute, set the attribute instead if name in SPECIAL_ATTRIBUTES and name in dir(self): - setattr(self, name, value) - return self + if getattr(self, name) is None: + setattr(self, name, value) + return self + else: + raise ValueError(f"'{name}' is a special attribute and does not " + f"support multi-property. It is already set " + f"and cannot be overwritten using this method. " + f"Please use direct assignment for setting this " + f"property after the first time.") + # ToDo: Implement the same behaviour for special attribute ids, + # https://gitlab.indiscale.com/caosdb/src/caosdb-pylib/-/issues/219 new_property = Property(name=name, id=id, description=description, datatype=datatype, value=value, unit=unit) diff --git a/unittests/test_issues.py b/unittests/test_issues.py index 5f44a90434f1de82dcd5be3959e2799f2f683fde..33fdc9e6bfd5e8034f6e54220f158ffe065b6225 100644 --- a/unittests/test_issues.py +++ b/unittests/test_issues.py @@ -143,3 +143,8 @@ def test_issue_134(): rec.add_property(name=attr, value=val) assert rec.__getattribute__(attr) == val assert rec.get_property(attr) is None + + exp_str = f"'{attr}' is a special attribute and does not support" + with raises(ValueError) as e: + rec.add_property(name=attr, value=val) + assert exp_str in str(e.value)