Skip to content

row_registry

Per-query identity map used when materializing joined database rows into models.

RowRegistry

Per-query identity map used while materializing joined rows.

A model reached through the same relation path with the same pk is built once and reused by every row it appears in, instead of being constructed per row and merged afterwards. Models below a many-to-many edge are scoped to their parent, because the through instance of that link is stored on the child. Pk-only models of not selected foreign keys are shared the same way. The registry lives only for one result set.

Attributes:

Name Type Description
instances dict[RegistryKey, Model]

built models keyed by (scope, table_prefix, pk), and pk-only models keyed by ((scope, table_prefix), field_name, pk)

subtree_has_multi dict[tuple[type, str], bool]

memo of whether the relations selected below a (model_cls, table_prefix) position contain a reverse or many-to-many relation that later rows can still extend

Source code in ormar/models/row_registry.py
Python
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class RowRegistry:
    """
    Per-query identity map used while materializing joined rows.

    A model reached through the same relation path with the same pk is built
    once and reused by every row it appears in, instead of being constructed
    per row and merged afterwards. Models below a many-to-many edge are scoped
    to their parent, because the through instance of that link is stored on
    the child. Pk-only models of not selected foreign keys are shared the same
    way. The registry lives only for one result set.

    :ivar instances: built models keyed by ``(scope, table_prefix, pk)``, and
        pk-only models keyed by ``((scope, table_prefix), field_name, pk)``
    :ivar subtree_has_multi: memo of whether the relations selected below a
        ``(model_cls, table_prefix)`` position contain a reverse or
        many-to-many relation that later rows can still extend
    """

    __slots__ = ("instances", "subtree_has_multi")

    def __init__(self) -> None:
        """
        Initialize an empty registry.
        """
        self.instances: dict[RegistryKey, "Model"] = {}
        self.subtree_has_multi: dict[tuple[type, str], bool] = {}

__init__()

Initialize an empty registry.

Source code in ormar/models/row_registry.py
Python
34
35
36
37
38
39
def __init__(self) -> None:
    """
    Initialize an empty registry.
    """
    self.instances: dict[RegistryKey, "Model"] = {}
    self.subtree_has_multi: dict[tuple[type, str], bool] = {}

selects_multi_relation(model_cls, related_models)

Check if selected relations below a model contain a reverse foreign key or a many-to-many relation, which are the only relations that a repeated row of an already built model can still add new children to.

Parameters:

Name Type Description Default
model_cls type[NewBaseModel]

model from which the selected relations start

required
related_models Any

selected relations as list or nested dict

required

Returns:

Type Description
bool

True if any selected relation on any level is multi or virtual

Source code in ormar/models/row_registry.py
Python
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def selects_multi_relation(
    model_cls: type["NewBaseModel"], related_models: Any
) -> bool:
    """
    Check if selected relations below a model contain a reverse foreign key
    or a many-to-many relation, which are the only relations that a repeated
    row of an already built model can still add new children to.

    :param model_cls: model from which the selected relations start
    :type model_cls: type[NewBaseModel]
    :param related_models: selected relations as list or nested dict
    :type related_models: Union[list, dict]
    :return: True if any selected relation on any level is multi or virtual
    :rtype: bool
    """
    model_fields = model_cls.ormar_config.model_fields
    for related in related_models:
        field = model_fields[related]
        if field.kind.many:
            return True
        if isinstance(related_models, dict) and related_models[related]:
            if selects_multi_relation(field.to, related_models[related]):
                return True
    return False