Key relationships
KeyNested takes the related entity's primary-key field as its
related_entity_field argument. It infers many=True from list annotations
such as list[Address] or list["Address"]. You can still pass many= to
set this explicitly.
Resolve a list of keys
Use origin when the input key name differs from the attribute name. This
example reads the entity_ids list and resolves each ID to a stored entity.
Access the resulting list through holder.entities.
from corm import Entity, Field, KeyNested, Storage
class SomeEntity(Entity):
id: int = Field(pk=True)
name: str
class EntityHolder(Entity):
name: str
entities: list[SomeEntity] = KeyNested(
related_entity_field=SomeEntity.id,
origin="entity_ids",
)
storage = Storage()
entity = SomeEntity({"id": 123, "name": "entity"}, storage=storage)
holder = EntityHolder({"entity_ids": [123], "name": "holder"}, storage=storage)
assert holder.entities == [entity]
Bidirectional key relationships
Set back_populates on KeyNested to name the Relationship on the
entity referenced by the key. The reverse field's annotation or many=
setting controls whether it returns a single owner or a list of owners.
An owner can be loaded before the related entity. Its reverse link is
available when that key appears in storage.
If the target inherits its primary-key field, annotate the KeyNested
attribute with the target subclass to select its Relationship.
from corm import (
Entity,
Field,
KeyNested,
Relationship,
Storage,
)
class SomeEntity(Entity):
id: int = Field(pk=True)
name: str
holder: "EntityHolder | None" = Relationship()
class EntityHolder(Entity):
name: str
entity: SomeEntity = KeyNested(
related_entity_field=SomeEntity.id,
origin="entity_id",
back_populates="holder",
)
storage = Storage()
holder = EntityHolder({"name": "holder", "entity_id": 123}, storage=storage)
entity = SomeEntity({"id": 123, "name": "entity"}, storage=storage)
assert holder.entity == entity
assert entity.holder == holder
Change relationships between entities
Writing through either field updates the owner's stored key. Assign an owner
to a single Relationship to move the link, or assign None to clear it.
For a reverse list, append or remove owners or assign the replacement list.
If the owning KeyNested field is a list, the operation adds or removes the
corresponding key. With required=False, edits to the list of resolved
entities preserve keys that are still missing. Assigning a replacement list
directly to the owning field replaces its stored keys.
from corm import Entity, Field, KeyNested, Relationship, Storage
class SomeEntity(Entity):
id: int = Field(pk=True)
name: str
holder: "EntityHolder | None" = Relationship()
class EntityHolder(Entity):
entity: SomeEntity | None = KeyNested(
related_entity_field=SomeEntity.id,
origin="entity_id",
back_populates="holder",
)
storage = Storage()
entity1 = SomeEntity({"id": 123, "name": "entity1"}, storage=storage)
entity2 = SomeEntity({"id": 321, "name": "entity2"}, storage=storage)
holder = EntityHolder(
data={"entity_id": 123},
storage=storage,
)
holder.entity = entity2
assert entity1.holder is None
assert entity2.holder is holder
assert holder.entity is entity2
assert holder.dict() == {"entity_id": 321}
new_holder = EntityHolder({"entity_id": None}, storage)
entity2.holder = new_holder
assert holder.entity is None
assert holder.dict() == {"entity_id": None}
assert new_holder.entity is entity2
assert new_holder.dict() == {"entity_id": 321}
Keys stored as objects
Pass a KeyManager when a reference is stored in a shape other than a bare
key. Implement get(data) to extract the hashable key used by Storage and
prepare_to_set(entity) to produce the stored representation when assigning
an entity. A list relation calls these methods for each reference. Return
None from prepare_to_set(None) for a cleared single reference.
This example reads and writes team member references stored as objects such
as {"id": 123}.
from corm import Entity, Field, KeyManager, KeyNested, Storage
class User(Entity):
id: int = Field(pk=True)
class UserKeyManager(KeyManager):
def get(self, data: dict[str, int]) -> int:
return data["id"]
def prepare_to_set(self, entity: Entity | None) -> dict[str, int] | None:
if entity is None:
return None
assert isinstance(entity, User)
return {"id": entity.id}
class Team(Entity):
members: list[User] = KeyNested(
User.id, origin="member_refs", key_manager=UserKeyManager()
)
storage = Storage()
amina = User({"id": 123}, storage)
lee = User({"id": 456}, storage)
team = Team({"member_refs": [{"id": 123}]}, storage)
assert team.members == [amina]
assert team.dict() == {"member_refs": [{"id": 123}]}
team.members = [amina, lee]
assert team.members == [amina, lee]
assert team.dict() == {"member_refs": [{"id": 123}, {"id": 456}]}