MCPcopy
hub / github.com/ormar-orm/ormar / _validate_flatten_leaves

Method _validate_flatten_leaves

ormar/models/excludable.py:678–714  ·  view source on GitHub ↗

Ensure every leaf addressed by a flatten spec is a real relation on the target model (not a scalar column and not a through model). :param items: set of leaf names being flattened on ``model_cls`` :type items: set :param model_cls: target model on which leav

(
        items: set, model_cls: type["Model"], path_parts: PathParts
    )

Source from the content-addressed store, hash-verified

676
677 @staticmethod
678 def _validate_flatten_leaves(
679 items: set, model_cls: type["Model"], path_parts: PathParts
680 ) -> None:
681 """
682 Ensure every leaf addressed by a flatten spec is a real relation on the
683 target model (not a scalar column and not a through model).
684
685 :param items: set of leaf names being flattened on ``model_cls``
686 :type items: set
687 :param model_cls: target model on which leaves must resolve to relations
688 :type model_cls: type[Model]
689 :param path_parts: tuple of segments leading to ``model_cls`` (only
690 joined for error messages, never parsed)
691 :type path_parts: PathParts
692 """
693 model_fields = model_cls.ormar_config.model_fields
694 for item in items:
695 related_field = model_fields.get(item)
696 if related_field is None:
697 raise QueryDefinitionError(
698 f"Unknown relation '{item}' on model "
699 f"{model_cls.get_name(lower=False)} in flatten_fields path "
700 f"'{join_path(path_parts, item)}'."
701 )
702 if getattr(related_field, "is_through", False):
703 raise QueryDefinitionError(
704 f"Cannot flatten through model '{item}' at path "
705 f"'{join_path(path_parts, item)}'. Flatten the many-to-many "
706 f"relation itself instead."
707 )
708 if not getattr(related_field, "is_relation", False):
709 raise QueryDefinitionError(
710 f"flatten_fields target '{join_path(path_parts, item)}' is "
711 f"not a relation on model {model_cls.get_name(lower=False)}. "
712 f"Only foreign keys, many-to-many, and reverse relations can "
713 f"be flattened."
714 )
715
716 def _validate_flatten_prefix_collisions(self) -> None:
717 """

Callers 1

_set_slotMethod · 0.95

Calls 4

join_pathFunction · 0.85
get_nameMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected