Files
ormar/docs/api/relations/alias-manager.md

3.3 KiB

relations.alias_manager

get_table_alias

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:

(str): randomly generated alias

AliasManager Objects

class AliasManager()

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

prefixed_columns

 | @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 (str): alias of given table
  • table (sqlalchemy.Table): table from which fields should be aliased
  • fields (Optional[List[str]]): fields to include

Returns:

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

prefixed_table_name

 | @staticmethod
 | prefixed_table_name(alias: str, name: str) -> text

Creates text clause with table name with aliased name.

Arguments:

  • alias (str): alias of given table
  • name (str): table name

Returns:

(sqlalchemy text clause): sqlalchemy text clause as "table_name aliased_name"

add_relation_type

 | add_relation_type(source_model: Type["Model"], relation_name: str, reverse_name: str = None, is_multi: bool = False) -> 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 (source Model): model with relation defined
  • relation_name (str): name of the relation to define
  • reverse_name (Optional[str]): name of related_name fo given relation for m2m relations
  • is_multi (bool): flag if relation being registered is a through m2m model

Returns:

(None): none

resolve_relation_alias

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

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

Arguments:

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

Returns:

(str): alias of the relation