128 lines
3.4 KiB
Markdown
128 lines
3.4 KiB
Markdown
<a name="relations.alias_manager"></a>
|
|
# relations.alias\_manager
|
|
|
|
<a name="relations.alias_manager.get_table_alias"></a>
|
|
#### get\_table\_alias
|
|
|
|
```python
|
|
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
|
|
|
|
<a name="relations.alias_manager.AliasManager"></a>
|
|
## AliasManager Objects
|
|
|
|
```python
|
|
class AliasManager()
|
|
```
|
|
|
|
Keep all aliases of relations between different tables.
|
|
One global instance is shared between all models.
|
|
|
|
<a name="relations.alias_manager.AliasManager.__init__"></a>
|
|
#### \_\_init\_\_
|
|
|
|
```python
|
|
| __init__() -> None
|
|
```
|
|
|
|
<a name="relations.alias_manager.AliasManager.prefixed_columns"></a>
|
|
#### prefixed\_columns
|
|
|
|
```python
|
|
| @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"
|
|
|
|
<a name="relations.alias_manager.AliasManager.prefixed_table_name"></a>
|
|
#### prefixed\_table\_name
|
|
|
|
```python
|
|
| @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"
|
|
|
|
<a name="relations.alias_manager.AliasManager.add_relation_type"></a>
|
|
#### add\_relation\_type
|
|
|
|
```python
|
|
| 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
|
|
|
|
<a name="relations.alias_manager.AliasManager.resolve_relation_alias"></a>
|
|
#### resolve\_relation\_alias
|
|
|
|
```python
|
|
| 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
|
|
|