Skip to content

Entity Id Modification Error

entity_id_modification_error

Error raised when code attempts to modify an entity's identity attribute.

Defines EntityIdModificationError, raised when an attempt is made to
change an entity's identity attribute (typically id) after it has already
been assigned. Once set, an entity's identity is immutable — modification
would break identity-based equality and hashing for aggregates.

Extends RuntimeErrorMixin and Error[MetadataValueType].

EntityIdModificationError

Bases: RuntimeErrorMixin, Error[MetadataValueType]

Raised when there is an attempt to modify an entity's identifier after it has been set.

Once assigned, an entity's identifier is immutable. Changing the
id would break the stability guarantee required for hash-based
collections, identity comparisons, and reliable persistence. This
error fires at the __setattr__ interception point whenever a
re-assignment of the identity field is detected.

Example
error = EntityIdModificationError("User", "id", 42)
# error.message = "Cannot modify 'id' of User once set (current value=42)."
Source code in src/forging_blocks/domain/errors/entity_id_modification_error.py
class EntityIdModificationError(RuntimeErrorMixin, Error[MetadataValueType]):
    """Raised when there is an attempt to modify an entity's identifier after it has been set.

    Once assigned, an entity's identifier is immutable. Changing the
    ``id`` would break the stability guarantee required for hash-based
    collections, identity comparisons, and reliable persistence. This
    error fires at the ``__setattr__`` interception point whenever a
    re-assignment of the identity field is detected.

    Example:
        ```python
        error = EntityIdModificationError("User", "id", 42)
        # error.message = "Cannot modify 'id' of User once set (current value=42)."
        ```
    """

    def __init__(self, class_name: str, attribute_name: str, current_value: object) -> None:
        """Initialise the error with the class, attribute, and current value.

        Args:
            class_name: Name of the class whose identifier was targeted.
            attribute_name: Name of the attribute that was being modified.
            current_value: The current (immutable) value of the identifier.

        """
        message = ErrorMessage(
            f"Cannot modify '{attribute_name}' of {class_name} once set "
            f"(current value={current_value!r})."
        )
        metadata: ErrorMetadata[MetadataValueType] = ErrorMetadata(
            {
                "class_name": class_name,
                "attribute_name": attribute_name,
                "current_value": current_value,
            }
        )
        super().__init__(message, metadata)

__init__(class_name: str, attribute_name: str, current_value: object) -> None

Initialise the error with the class, attribute, and current value.

Parameters:

Name Type Description Default
class_name str

Name of the class whose identifier was targeted.

required
attribute_name str

Name of the attribute that was being modified.

required
current_value object

The current (immutable) value of the identifier.

required
Source code in src/forging_blocks/domain/errors/entity_id_modification_error.py
def __init__(self, class_name: str, attribute_name: str, current_value: object) -> None:
    """Initialise the error with the class, attribute, and current value.

    Args:
        class_name: Name of the class whose identifier was targeted.
        attribute_name: Name of the attribute that was being modified.
        current_value: The current (immutable) value of the identifier.

    """
    message = ErrorMessage(
        f"Cannot modify '{attribute_name}' of {class_name} once set "
        f"(current value={current_value!r})."
    )
    metadata: ErrorMetadata[MetadataValueType] = ErrorMetadata(
        {
            "class_name": class_name,
            "attribute_name": attribute_name,
            "current_value": current_value,
        }
    )
    super().__init__(message, metadata)