Nested relationships
Nested() and Relationship() infer the related entity type and whether the
field holds one entity or a list. Use Address for a single entity and
list[Address] for a list. Quoted forward references such as "Address" and
list["Address"] are resolved through the entity registry when needed. Optional
annotations such as Address | None and Optional[list[Address]] infer the
same type and cardinality as their non-optional forms.
You can still pass entity_type= and many= explicitly. Each explicit
argument overrides the corresponding inference.
Simple relationship
Nested wraps an embedded dictionary as an entity, so a user can access the
address fields through user.address.
from corm import Entity, Nested, Storage
class Address(Entity):
street: str
number: int
class User(Entity):
name: str
address: Address = Nested()
storage = Storage()
john = User(
data={
"name": "John",
"address": {
"street": "First",
"number": 1,
},
},
storage=storage,
)
assert john.address.street == "First"
assert john.address.number == 1
Bidirectional relationships
Set back_populates on the Nested field to name the Relationship on
the related entity. Each pair has its own links, so fields such as
Order.billing and Order.shipping can both point to Address and
have separate reverse fields.
The reverse field's annotation or many= setting controls whether it
returns one owner or a list of owners. corm resolves the pair when the first
owning entity is created, which allows forward references. Target subclasses
can use the pair by inheriting its Relationship.
When a redefined owner class takes over a back_populates pair, instances of
the superseded class are stale and their behavior is undefined.
from corm import Entity, Nested, Relationship, Storage
class Address(Entity):
street: str
number: int
user: "User | None" = Relationship()
class User(Entity):
name: str
address: Address | None = Nested(back_populates="user")
storage = Storage()
john = User(
data={
"name": "John",
"address": {
"street": "First",
"number": 1,
},
},
storage=storage,
)
assert john.address is not None
assert john.address.user == john
Self-nested
A pair can connect two fields on the same entity class. Appending a child to
another parent's list or assigning its parent moves it from its old parent.
from corm import Entity, Nested, Relationship, Storage
class Item(Entity):
id: int
items: list["Item"] = Nested(back_populates="parent", default=list)
parent: "Item | None" = Relationship()
storage = Storage()
item1 = Item({"id": 1, "items": [{"id": 2}, {"id": 3}]}, storage)
item2, item3 = item1.items
assert item1.id == 1
assert item1.parent is None
assert item2.id == 2
assert item2.parent == item1
assert item2.items == []
assert item3.id == 3
assert item3.parent == item1
assert item3.items == []
item3.items.append(item2)
assert item2.parent is item3
assert item1.items == [item3]
item2.parent = item1
assert item1.items == [item3, item2]
assert item3.items == []
Change relationships between entities
You can write through either field. Assign a new owner to a single
Relationship to move the related entity, or assign None to unlink it.
If the new owner holds a single value, the move replaces that value and
clears the displaced entity's reverse link.
from corm import Entity, Nested, Relationship, Storage
class Address(Entity):
street: str
user: "User | None" = Relationship()
class User(Entity):
id: int
address: Address | None = Nested(back_populates="user")
storage = Storage()
john = User(
data={
"id": 1,
"name": "John",
"description": "john smith",
"address": {
"street": "kirova",
},
},
storage=storage,
)
old_address = john.address
assert old_address is not None
assert old_address.user is john
address = Address(data={"street": "lenina"}, storage=storage)
john.address = address
assert old_address.user is None
assert address.user is john
assert john.address is address
jane = User({"id": 2, "address": {"street": "First"}}, storage)
displaced = jane.address
assert displaced is not None
address.user = jane
assert john.address is None
assert jane.address is address
assert address.user is jane
assert displaced.user is None
address.user = None
assert jane.address is None
assert address.user is None
On either side, a list can be edited in place or replaced. Appending or removing an owner through a reverse list updates that owner's field.
from corm import Entity, Nested, Relationship, Storage
class Address(Entity):
street: str
users: list["User"] = Relationship()
class User(Entity):
id: int
addresses: list[Address] = Nested(back_populates="users", default=list)
storage = Storage()
john = User(
data={
"id": 1,
"name": "John",
"description": "john smith",
"addresses": [
{
"street": "kirova 1",
},
{
"street": "kirova 2",
},
],
},
storage=storage,
)
old_address1, old_address2 = john.addresses
# you can change existing list
john.addresses.remove(old_address1)
assert old_address1.users == []
assert old_address2.users == [john]
john.addresses.clear()
assert old_address1.users == []
assert old_address2.users == []
assert john.addresses == []
new_address1 = Address(data={"street": "lenina 1"}, storage=storage)
new_address2 = Address(data={"street": "lenina 2"}, storage=storage)
john.addresses.append(new_address1)
assert new_address1.users == [john]
assert john.addresses == [new_address1]
john.addresses.extend([new_address2])
assert new_address2.users == [john]
assert john.addresses == [new_address1, new_address2]
# or completely replace it
john.addresses = [old_address1]
assert new_address1.users == []
assert new_address2.users == []
assert old_address1.users == [john]
jane = User({"id": 2}, storage)
old_address1.users.append(jane)
assert jane.addresses == [old_address1]
old_address1.users.remove(john)
assert john.addresses == []
old_address1.users = [john]
assert jane.addresses == []
assert john.addresses == [old_address1]
The older back_relation parameter still works and emits a
DeprecationWarning. To migrate, replace it with
back_populates="reverse_field_name".
Manually related entities
A standalone Relationship uses relation_type to read and write links
managed directly by Storage. This example links a user to an address and
then removes the link.
from corm import Entity, Relationship, RelationType, Storage
storage = Storage()
class Address(Entity):
street: str
number: int
class User(Entity):
name: str
address: Address | None = Relationship(relation_type=RelationType.PARENT)
address = Address({"street": "First", "number": 1}, storage)
john = User({"name": "John"}, storage)
storage.make_relation(
from_=john,
to_=address,
relation_type=RelationType.PARENT,
)
assert john.address == address
storage.remove_relation(
from_=john,
to_=address,
relation_type=RelationType.PARENT,
)
assert john.address is None