Relation

An immutable data structure representing a link between two entities.

Overview

The Relation class is a frozen dataclass designed to model directed connections within a graph or relational system. Since it is frozen, instances are hashable and their state cannot be modified after creation, ensuring data integrity.

from dataclasses import dataclass
@dataclass(frozen=True)

Attributes

Attribute Type Default Description
from_id str Required The unique identifier of the source entity.
to_id str Required The unique identifier of the target entity.
connection_type str "relation" Category or label describing the nature of the link.
metadata Dict[str, Any] {} Additional key-value pairs for custom properties.

Internal Logic

__post_init__(self)

Performs post-instantiation validation. If either from_id or to_id are empty or null, it raises a ValueError.

Exception Raised: ValueError
"Una relación requiere un ID de origen y un ID de destino."

Example Usage

Basic Instance

rel = Relation(from_id="user_1", to_id="order_50")

Custom Metadata

rel = Relation("A", "B", "friendship", {"since": 2024})