Skip to content

relations

expand_reverse_relationship(model_field)

If the reverse relation has not been set before it's set here.

Parameters:

Name Type Description Default
model_field ForeignKeyField
required

Returns:

Type Description
None

None

Source code in ormar/models/helpers/relations.py
Python
35
36
37
38
39
40
41
42
43
44
45
def expand_reverse_relationship(model_field: "ForeignKeyField") -> None:
    """
    If the reverse relation has not been set before it's set here.

    :param model_field:
    :type model_field:
    :return: None
    :rtype: None
    """
    if reverse_field_not_already_registered(model_field=model_field):
        register_reverse_model_fields(model_field=model_field)

expand_reverse_relationships(model)

Iterates through model_fields of given model and verifies if all reverse relation have been populated on related models.

If the reverse relation has not been set before it's set here.

Parameters:

Name Type Description Default
model type[Model]

model on which relation should be checked and registered

required
Source code in ormar/models/helpers/relations.py
Python
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def expand_reverse_relationships(model: type["Model"]) -> None:
    """
    Iterates through model_fields of given model and verifies if all reverse
    relation have been populated on related models.

    If the reverse relation has not been set before it's set here.

    :param model: model on which relation should be checked and registered
    :type model: Model class
    """
    model_fields = list(model.ormar_config.model_fields.values())
    for model_field in model_fields:
        if (
            model_field.is_relation
            and not model_field.has_unresolved_forward_refs()
            and not model_field.is_through
        ):
            model_field = cast("ForeignKeyField", model_field)
            expand_reverse_relationship(model_field=model_field)

register_reverse_model_fields(model_field)

Registers reverse ForeignKey field on related model. By default it's name.lower()+'s' of the model on which relation is defined.

But if the related_model name is provided it's registered with that name. Autogenerated reverse fields also set related_name to the original field name.

Parameters:

Name Type Description Default
model_field ForeignKeyField

original relation ForeignKey field

required
Source code in ormar/models/helpers/relations.py
Python
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
def register_reverse_model_fields(model_field: "ForeignKeyField") -> None:
    """
    Registers reverse ForeignKey field on related model.
    By default it's name.lower()+'s' of the model on which relation is defined.

    But if the related_model name is provided it's registered with that name.
    Autogenerated reverse fields also set related_name to the original field name.

    :param model_field: original relation ForeignKey field
    :type model_field: relation Field
    """
    related_name = model_field.get_related_name()
    reset_relation_caches(model=model_field.to)
    related_model_fields = model_field.to.ormar_config.model_fields
    if model_field.is_multi:
        resolve_through_fields(model_field=cast("ManyToManyField", model_field))
        related_model_fields[related_name] = ManyToMany(  # type: ignore
            model_field.owner,
            through=model_field.through,
            name=related_name,
            virtual=True,
            related_name=model_field.name,
            owner=model_field.to,
            self_reference=model_field.self_reference,
            self_reference_primary=model_field.self_reference_primary,
            orders_by=model_field.related_orders_by,
            skip_field=model_field.skip_reverse,
            through_relation_name=model_field.through_reverse_relation_name,
            through_reverse_relation_name=model_field.through_relation_name,
            through_relation_nullable=model_field.through_reverse_relation_nullable,
            through_reverse_relation_nullable=model_field.through_relation_nullable,
        )
        # register foreign keys on through model
        model_field = cast("ManyToManyField", model_field)
        register_through_shortcut_fields(model_field=model_field)
        adjust_through_many_to_many_model(model_field=model_field)
    else:
        related_model_fields[related_name] = ForeignKey(  # type: ignore
            model_field.owner,
            real_name=related_name,
            virtual=True,
            related_name=model_field.name,
            owner=model_field.to,
            self_reference=model_field.self_reference,
            orders_by=model_field.related_orders_by,
            skip_field=model_field.skip_reverse,
            one_to_one=model_field.one_to_one,
        )
    if not model_field.skip_reverse:
        reverse_field = related_model_fields[related_name]
        field_type = reverse_field_annotation(
            kind=reverse_field.kind,
            annotation=reverse_field.__type__,
        )
        model_field.to.model_fields[related_name] = FieldInfo.from_annotated_attribute(
            annotation=field_type, default=None
        )
        add_field_serializer_for_reverse_relations(
            to_model=model_field.to, related_name=related_name
        )
        mark_for_rebuild(model=model_field.to)
        setattr(model_field.to, related_name, RelationDescriptor(name=related_name))

register_through_shortcut_fields(model_field)

Registers m2m relation through shortcut on both ends of the relation.

Parameters:

Name Type Description Default
model_field ManyToManyField

relation field defined in parent model

required
Source code in ormar/models/helpers/relations.py
Python
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
def register_through_shortcut_fields(model_field: "ManyToManyField") -> None:
    """
    Registers m2m relation through shortcut on both ends of the relation.

    :param model_field: relation field defined in parent model
    :type model_field: ManyToManyField
    """
    through_model = model_field.through
    through_name = through_model.get_name(lower=True)
    related_name = model_field.get_related_name()
    for model in (model_field.owner, model_field.to):
        existing = model.ormar_config.model_fields.get(through_name)
        if existing is not None and not existing.is_through:
            raise ModelDefinitionError(
                f"Field {through_name} of {model.get_name(lower=False)} collides "
                f"with the shortcut to the through model {through_model.__name__}, "
                "rename the relation."
            )

    model_field.owner.ormar_config.model_fields[through_name] = Through(
        through_model,
        real_name=through_name,
        virtual=True,
        related_name=model_field.name,
        owner=model_field.owner,
        nullable=True,
    )

    model_field.to.ormar_config.model_fields[through_name] = Through(
        through_model,
        real_name=through_name,
        virtual=True,
        related_name=related_name,
        owner=model_field.to,
        nullable=True,
    )
    setattr(model_field.owner, through_name, RelationDescriptor(name=through_name))
    setattr(model_field.to, through_name, RelationDescriptor(name=through_name))

reset_relation_caches(model)

Drop the lazily cached relation metadata of a model that gains a reverse relation, so models already used before a related model was defined see the new relation (and lose trusted row construction). The cached relation paths of every model connected to it are rebuilt on next use too.

Parameters:

Name Type Description Default
model type[Model]

model receiving a reverse relation

required
Source code in ormar/models/helpers/relations.py
Python
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def reset_relation_caches(model: type["Model"]) -> None:
    """
    Drop the lazily cached relation metadata of a model that gains a reverse
    relation, so models already used before a related model was defined see
    the new relation (and lose trusted row construction). The cached relation
    paths of every model connected to it are rebuilt on next use too.

    :param model: model receiving a reverse relation
    :type model: type[Model]
    """
    model._related_names = None
    model._related_fields = None
    model._related_field_map = None
    model._trusted_rows = None
    RelationGraph.changed()

resolve_through_fields(model_field)

Adopt the foreign keys a user-defined through model already declares to the owner and the target of a many-to-many relation (e.g. a legacy link table keyed by them), so ormar uses those fields instead of adding its own. Explicit through_relation_name / through_reverse_relation_name must name foreign keys to the owner / target when the through model declares them; several foreign keys to one side, and any self-referencing link table, need those names.

Parameters:

Name Type Description Default
model_field ManyToManyField

many-to-many relation field

required

Raises:

Type Description
ModelDefinitionError

if the through model has relations to other models or unresolved forward references, if an explicit name points at the wrong field, or if the foreign keys are ambiguous

Source code in ormar/models/helpers/relations.py
Python
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
def resolve_through_fields(model_field: "ManyToManyField") -> None:
    """
    Adopt the foreign keys a user-defined through model already declares to the
    owner and the target of a many-to-many relation (e.g. a legacy link table
    keyed by them), so ormar uses those fields instead of adding its own.
    Explicit ``through_relation_name`` / ``through_reverse_relation_name`` must
    name foreign keys to the owner / target when the through model declares
    them; several foreign keys to one side, and any self-referencing link
    table, need those names.

    :raises ModelDefinitionError: if the through model has relations to other
        models or unresolved forward references, if an explicit name points at
        the wrong field, or if the foreign keys are ambiguous
    :param model_field: many-to-many relation field
    :type model_field: ManyToManyField
    """
    through = model_field.through
    through_name = through.get_name(lower=False)
    through_fields = through.ormar_config.model_fields
    relations = {
        name: field
        for name, field in through_fields.items()
        if field.is_relation and not field.virtual
    }
    if not relations:
        return
    unresolved = [
        name for name, field in relations.items() if field.to.__class__ == ForwardRef
    ]
    if unresolved:
        raise ModelDefinitionError(
            f"Through model {through_name} has unresolved forward references "
            f"{unresolved}, call {through_name}.update_forward_refs() before the "
            "relation using it is completed."
        )
    allowed = (model_field.owner, model_field.to)
    foreign = [name for name, field in relations.items() if field.to not in allowed]
    if foreign:
        raise ModelDefinitionError(
            f"Through model {through_name} can only have relations to "
            f"{model_field.owner.get_name(lower=False)} and "
            f"{model_field.to.get_name(lower=False)}, got {foreign}."
        )
    if model_field.owner is model_field.to and not (
        model_field.through_relation_name and model_field.through_reverse_relation_name
    ):
        raise ModelDefinitionError(
            f"Self-referencing relation through {through_name} needs "
            "through_relation_name and through_reverse_relation_name naming its "
            "foreign keys."
        )
    for attribute, side in (
        ("through_relation_name", model_field.owner),
        ("through_reverse_relation_name", model_field.to),
    ):
        resolve_through_side(
            model_field=model_field, relations=relations, attribute=attribute, side=side
        )

resolve_through_side(model_field, relations, attribute, side)

Validate or pick the through model foreign key leading to one side of a many-to-many relation and store its name on the relation field.

Parameters:

Name Type Description Default
model_field ManyToManyField

many-to-many relation field

required
relations dict[str, BaseField]

non-virtual relation fields of the through model by name

required
attribute str

through_relation_name or through_reverse_relation_name

required
side type[Model]

model the foreign key must lead to

required

Raises:

Type Description
ModelDefinitionError

if an explicit name is not a foreign key to the side, or several foreign keys lead to it without an explicit name

Source code in ormar/models/helpers/relations.py
Python
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
def resolve_through_side(
    model_field: "ManyToManyField",
    relations: dict[str, "BaseField"],
    attribute: str,
    side: type["Model"],
) -> None:
    """
    Validate or pick the through model foreign key leading to one side of a
    many-to-many relation and store its name on the relation field.

    :raises ModelDefinitionError: if an explicit name is not a foreign key to
        the side, or several foreign keys lead to it without an explicit name
    :param model_field: many-to-many relation field
    :type model_field: ManyToManyField
    :param relations: non-virtual relation fields of the through model by name
    :type relations: dict[str, BaseField]
    :param attribute: ``through_relation_name`` or
        ``through_reverse_relation_name``
    :type attribute: str
    :param side: model the foreign key must lead to
    :type side: type[Model]
    """
    through_name = model_field.through.get_name(lower=False)
    name = getattr(model_field, attribute)
    if name is not None:
        existing = model_field.through.ormar_config.model_fields.get(name)
        if existing is not None and (
            name not in relations or relations[name].to is not side
        ):
            raise ModelDefinitionError(
                f"{attribute} '{name}' must be a foreign key of {through_name} to "
                f"{side.get_name(lower=False)}."
            )
        return
    candidates = [field_name for field_name, f in relations.items() if f.to is side]
    if len(candidates) > 1:
        raise ModelDefinitionError(
            f"Through model {through_name} has several foreign keys to "
            f"{side.get_name(lower=False)} ({candidates}), pass {attribute} to "
            "choose one."
        )
    if candidates:
        setattr(model_field, attribute, candidates[0])

reverse_field_annotation(kind, annotation)

Pydantic annotation of a reverse relation field: a reverse foreign key accepts one model or a list of them, a reverse one-to-one a single model, and a many-to-many keeps the annotation of its field.

Parameters:

Name Type Description Default
kind RelationKind

kind of the reverse relation field

required
annotation Any

annotation of the reverse field

required

Returns:

Type Description
Any

annotation to register on the related model

Source code in ormar/models/helpers/relations.py
Python
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def reverse_field_annotation(kind: RelationKind, annotation: Any) -> Any:
    """
    Pydantic annotation of a reverse relation field: a reverse foreign key
    accepts one model or a list of them, a reverse one-to-one a single model,
    and a many-to-many keeps the annotation of its field.

    :param kind: kind of the reverse relation field
    :type kind: RelationKind
    :param annotation: annotation of the reverse field
    :type annotation: Any
    :return: annotation to register on the related model
    :rtype: Any
    """
    if kind is REVERSE_FOREIGN_KEY:
        return Union[annotation, list[annotation], None]
    if kind is REVERSE_ONE_TO_ONE:
        return Optional[annotation]
    return annotation

reverse_field_not_already_registered(model_field)

Checks if the reverse side of the relation is not yet registered on the related model, i.e. no field named as the relation's related_name exists there.

Parameters:

Name Type Description Default
model_field ForeignKeyField

original relation ForeignKey field

required

Returns:

Type Description
bool

result of the check

Raises:

Type Description
ModelDefinitionError

if related name is already used but lead to different related model

Source code in ormar/models/helpers/relations.py
Python
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
def reverse_field_not_already_registered(model_field: "ForeignKeyField") -> bool:
    """
    Checks if the reverse side of the relation is not yet registered on the
    related model, i.e. no field named as the relation's related_name exists there.

    :raises ModelDefinitionError: if related name is already used but lead to different
    related model
    :param model_field: original relation ForeignKey field
    :type model_field: relation Field
    :return: result of the check
    :rtype: bool
    """
    related_name = model_field.get_related_name()
    if related_name not in model_field.to.ormar_config.model_fields:
        return True
    verify_related_name_dont_duplicate(
        related_name=related_name, model_field=model_field
    )
    return False

Repeats the related_name checks deferred while given field was leading to an unresolved ForwardRef, now that its target model is known.

Parameters:

Name Type Description Default
model_field ForeignKeyField

just resolved relation field

required

Returns:

Type Description
None

None

Raises:

Type Description
ModelDefinitionError

if the field's name was used as related_name by a relation leading from a different model

Source code in ormar/models/helpers/relations.py
Python
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
def verify_deferred_related_names(model_field: "ForeignKeyField") -> None:
    """
    Repeats the related_name checks deferred while given field was leading to an
    unresolved ForwardRef, now that its target model is known.

    :raises ModelDefinitionError: if the field's name was used as related_name by a
    relation leading from a different model
    :param model_field: just resolved relation field
    :type model_field: relation Field
    :return: None
    :rtype: None
    """
    deferred = model_field.deferred_related_name_checks
    model_field.deferred_related_name_checks = []
    for claiming_field in deferred:
        verify_related_name_dont_duplicate(
            related_name=model_field.name, model_field=claiming_field
        )

Verifies whether the used related_name (regardless of the fact if user defined or auto generated) is already used on related model, but is connected with other model than the one that we connect right now.

A field still leading to an unresolved ForwardRef cannot be compared yet, so the check is deferred and repeated by verify_deferred_related_names once the field is resolved.

Parameters:

Name Type Description Default
related_name str
required
model_field ForeignKeyField

original relation ForeignKey field

required

Returns:

Type Description
None

None

Raises:

Type Description
ModelDefinitionError

if name is already used but lead to different related model

Source code in ormar/models/helpers/relations.py
Python
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
def verify_related_name_dont_duplicate(
    related_name: str, model_field: "ForeignKeyField"
) -> None:
    """
    Verifies whether the used related_name (regardless of the fact if user defined or
    auto generated) is already used on related model, but is connected with other model
    than the one that we connect right now.

    A field still leading to an unresolved ForwardRef cannot be compared yet, so the
    check is deferred and repeated by ``verify_deferred_related_names`` once the
    field is resolved.

    :raises ModelDefinitionError: if name is already used but lead to different related
    model
    :param related_name:
    :type related_name:
    :param model_field: original relation ForeignKey field
    :type model_field: relation Field
    :return: None
    :rtype: None
    """
    fk_field = model_field.to.ormar_config.model_fields.get(related_name)
    if not fk_field:  # pragma: no cover
        return
    if fk_field.has_unresolved_forward_refs():
        cast("ForeignKeyField", fk_field).deferred_related_name_checks.append(
            model_field
        )
        return
    if (
        fk_field.to != model_field.owner
        and fk_field.to.ormar_config != model_field.owner.ormar_config
    ):
        raise ormar.ModelDefinitionError(
            f"Relation with related_name "
            f"'{related_name}' "
            f"leading to model "
            f"{model_field.to.get_name(lower=False)} "
            f"cannot be used on model "
            f"{model_field.owner.get_name(lower=False)} "
            f"because it's already used by model "
            f"{fk_field.to.get_name(lower=False)}"
        )