Skip to content

relations.alias_manager

get_table_alias

1
get_table_alias() -> str

Creates a random string that is used to alias tables in joins. It's necessary that each relation has it's own aliases cause you can link to the same target tables from multiple fields on one model as well as from multiple different models in one join.

Returns:

randomly generated alias :rtype: str

AliasManager Objects

1
class AliasManager()

Keep all aliases of relations between different tables. One global instance is shared between all models.

reversed_aliases

1
2
 | @property
 | reversed_aliases() -> Dict

Returns swapped key-value pairs from aliases where alias is the key.

Returns:

dictionary of prefix to relation :rtype: Dict

prefixed_columns

1
2
 | @staticmethod
 | prefixed_columns(alias: str, table: sqlalchemy.Table, fields: List = None) -> List[text]

Creates a list of aliases sqlalchemy text clauses from string alias and sqlalchemy.Table.

Optional list of fields to include can be passed to extract only those columns. List has to have sqlalchemy names of columns (ormar aliases) not the ormar ones.

Arguments:

  • alias: alias of given table :type alias: str
  • table: table from which fields should be aliased :type table: sqlalchemy.Table
  • fields: fields to include :type fields: Optional[List[str]]

Returns:

list of sqlalchemy text clauses with "column name as aliased name" :rtype: List[text]

prefixed_table_name

1
2
 | @staticmethod
 | prefixed_table_name(alias: str, table: sqlalchemy.Table) -> text

Creates text clause with table name with aliased name.

Arguments:

  • alias: alias of given table :type alias: str
  • table: table :type table: sqlalchemy.Table

Returns:

sqlalchemy text clause as "table_name aliased_name" :rtype: sqlalchemy text clause

add_relation_type

1
 | add_relation_type(source_model: Type["Model"], relation_name: str, reverse_name: str = None) -> None

Registers the relations defined in ormar models. Given the relation it registers also the reverse side of this relation.

Used by both ForeignKey and ManyToMany relations.

Each relation is registered as Model name and relation name. Each alias registered has to be unique.

Aliases are used to construct joins to assure proper links between tables. That way you can link to the same target tables from multiple fields on one model as well as from multiple different models in one join.

Arguments:

  • source_model: model with relation defined :type source_model: source Model
  • relation_name: name of the relation to define :type relation_name: str
  • reverse_name: name of related_name fo given relation for m2m relations :type reverse_name: Optional[str]

Returns:

none :rtype: None

add_alias

1
 | add_alias(alias_key: str) -> str

Adds alias to the dictionary of aliases under given key.

Arguments:

  • alias_key: key of relation to generate alias for :type alias_key: str

Returns:

generated alias :rtype: str

resolve_relation_alias

1
 | resolve_relation_alias(from_model: Union[Type["Model"], Type["ModelRow"]], relation_name: str) -> str

Given model and relation name returns the alias for this relation.

Arguments:

  • from_model: model with relation defined :type from_model: source Model
  • relation_name: name of the relation field :type relation_name: str

Returns:

alias of the relation :rtype: str

resolve_relation_alias_after_complex

1
 | resolve_relation_alias_after_complex(source_model: Union[Type["Model"], Type["ModelRow"]], relation_str: str, relation_field: "ForeignKeyField") -> str

Given source model and relation string returns the alias for this complex relation if it exists, otherwise fallback to normal relation from a relation field definition.

Arguments:

  • relation_field: field with direct relation definition :type relation_field: "ForeignKeyField"
  • source_model: model with query starts :type source_model: source Model
  • relation_str: string with relation joins defined :type relation_str: str

Returns:

alias of the relation :rtype: str