Skip to content

Entity Id Deletion Error

entity_id_deletion_error

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

Defines EntityIdDeletionError, raised when an entity's id field is
targeted for deletion. An entity's id defines its identity and must
never be deleted after being set.

Extends RuntimeErrorMixin and Error[MetadataValueType].

EntityIdDeletionError

Bases: RuntimeErrorMixin, Error[MetadataValueType]

Raised when there is an attempt to delete an entity's identifier.

An entity's identity must remain stable for its lifetime. Deleting
the id field would break identity-based equality, hash lookups,
and aggregate consistency. This error fires at the point where
__delattr__ on the identifier is intercepted.

Example
error = EntityIdDeletionError("User")
# error.message = "Cannot delete 'id' of User as it defines the entity's identity."
Source code in src/forging_blocks/domain/errors/entity_id_deletion_error.py
class EntityIdDeletionError(RuntimeErrorMixin, Error[MetadataValueType]):
    """Raised when there is an attempt to delete an entity's identifier.

    An entity's identity must remain stable for its lifetime. Deleting
    the ``id`` field would break identity-based equality, hash lookups,
    and aggregate consistency. This error fires at the point where
    ``__delattr__`` on the identifier is intercepted.

    Example:
        ```python
        error = EntityIdDeletionError("User")
        # error.message = "Cannot delete 'id' of User as it defines the entity's identity."
        ```
    """

    def __init__(self, class_name: str) -> None:
        """Initialise the error with the class name.

        Args:
            class_name: Name of the class whose identifier was targeted for deletion.

        """
        message = ErrorMessage(
            f"Cannot delete 'id' of {class_name} as it defines the entity's identity."
        )
        metadata: ErrorMetadata[MetadataValueType] = ErrorMetadata(
            {
                "class_name": class_name,
                "attribute_name": "id",
            }
        )
        super().__init__(message, metadata)

__init__(class_name: str) -> None

Initialise the error with the class name.

Parameters:

Name Type Description Default
class_name str

Name of the class whose identifier was targeted for deletion.

required
Source code in src/forging_blocks/domain/errors/entity_id_deletion_error.py
def __init__(self, class_name: str) -> None:
    """Initialise the error with the class name.

    Args:
        class_name: Name of the class whose identifier was targeted for deletion.

    """
    message = ErrorMessage(
        f"Cannot delete 'id' of {class_name} as it defines the entity's identity."
    )
    metadata: ErrorMetadata[MetadataValueType] = ErrorMetadata(
        {
            "class_name": class_name,
            "attribute_name": "id",
        }
    )
    super().__init__(message, metadata)