rename alias manager

This commit is contained in:
collerek
2020-08-26 14:33:46 +02:00
parent 63a24e7d36
commit a9f88e8f8f
8 changed files with 11 additions and 20 deletions

BIN
.coverage

Binary file not shown.

View File

@ -79,13 +79,13 @@ But don't worry - ormar can handle situations like this, as it uses the Relation
Each class is registered with the same instance of the AliasManager that you can access like this: Each class is registered with the same instance of the AliasManager that you can access like this:
```python ```python
SchoolClass._orm_relationship_manager SchoolClass.alias_manager
``` ```
It's the same object for all `Models` It's the same object for all `Models`
```python ```python
print(Teacher._orm_relationship_manager == Student._orm_relationship_manager) print(Teacher.alias_manager == Student.alias_manager)
# will produce: True # will produce: True
``` ```
@ -94,11 +94,11 @@ print(Teacher._orm_relationship_manager == Student._orm_relationship_manager)
You can even preview the alias used for any relation by passing two tables names. You can even preview the alias used for any relation by passing two tables names.
```python ```python
print(Teacher._orm_relationship_manager.resolve_relation_join( print(Teacher.alias_manager.resolve_relation_join(
'students', 'categories')) 'students', 'categories'))
# will produce: KId1c6 (sample value) # will produce: KId1c6 (sample value)
print(Teacher._orm_relationship_manager.resolve_relation_join( print(Teacher.alias_manager.resolve_relation_join(
'categories', 'students')) 'categories', 'students'))
# will produce: EFccd5 (sample value) # will produce: EFccd5 (sample value)
``` ```

View File

@ -54,6 +54,7 @@ def ForeignKey(
class ForeignKeyField(BaseField): class ForeignKeyField(BaseField):
to: Type["Model"] to: Type["Model"]
name: str
related_name: str related_name: str
virtual: bool virtual: bool
@ -65,15 +66,6 @@ class ForeignKeyField(BaseField):
def validate(cls, value: Any) -> Any: def validate(cls, value: Any) -> Any:
return value return value
# @property
# def __type__(self) -> Type[BaseModel]:
# return self.to.__pydantic_model__
# @classmethod
# def get_column_type(cls) -> sqlalchemy.Column:
# to_column = cls.to.Meta.model_fields[cls.to.Meta.pkname]
# return to_column.column_type
@classmethod @classmethod
def _extract_model_from_sequence( def _extract_model_from_sequence(
cls, value: List, child: "Model" cls, value: List, child: "Model"

View File

@ -26,7 +26,7 @@ class ModelMeta:
columns: List[sqlalchemy.Column] columns: List[sqlalchemy.Column]
pkname: str pkname: str
model_fields: Dict[str, Union[BaseField, ForeignKey]] model_fields: Dict[str, Union[BaseField, ForeignKey]]
_orm_relationship_manager: AliasManager alias_manager: AliasManager
def register_relation_on_build(table_name: str, field: ForeignKey, name: str) -> None: def register_relation_on_build(table_name: str, field: ForeignKey, name: str) -> None:
@ -158,7 +158,7 @@ class ModelMetaclass(pydantic.main.ModelMetaclass):
) )
expand_reverse_relationships(new_model) expand_reverse_relationships(new_model)
new_model.Meta._orm_relationship_manager = relationship_manager new_model.Meta.alias_manager = relationship_manager
new_model.objects = QuerySet(new_model) new_model.objects = QuerySet(new_model)
return new_model return new_model

View File

@ -40,7 +40,7 @@ class Model(NewBaseModel):
if select_related: if select_related:
related_models = group_related_list(select_related) related_models = group_related_list(select_related)
table_prefix = cls.Meta._orm_relationship_manager.resolve_relation_join( table_prefix = cls.Meta.alias_manager.resolve_relation_join(
previous_table, cls.Meta.table.name previous_table, cls.Meta.table.name
) )

View File

@ -109,7 +109,7 @@ class QueryClause:
previous_table = model_cls.Meta.tablename previous_table = model_cls.Meta.tablename
for part in related_parts: for part in related_parts:
current_table = model_cls.Meta.model_fields[part].to.Meta.tablename current_table = model_cls.Meta.model_fields[part].to.Meta.tablename
manager = model_cls.Meta._orm_relationship_manager manager = model_cls.Meta.alias_manager
table_prefix = manager.resolve_relation_join(previous_table, current_table) table_prefix = manager.resolve_relation_join(previous_table, current_table)
model_cls = model_cls.Meta.model_fields[part].to model_cls = model_cls.Meta.model_fields[part].to
previous_table = current_table previous_table = current_table

View File

@ -44,7 +44,7 @@ class Query:
@property @property
def relation_manager(self) -> AliasManager: def relation_manager(self) -> AliasManager:
return self.model_cls.Meta._orm_relationship_manager return self.model_cls.Meta.alias_manager
def build_select_expression(self) -> Tuple[sqlalchemy.sql.select, List[str]]: def build_select_expression(self) -> Tuple[sqlalchemy.sql.select, List[str]]:
self.columns = list(self.table.columns) self.columns = list(self.table.columns)
@ -84,7 +84,7 @@ class Query:
model_cls = join_params.model_cls.Meta.model_fields[part].to model_cls = join_params.model_cls.Meta.model_fields[part].to
to_table = model_cls.Meta.table.name to_table = model_cls.Meta.table.name
alias = model_cls.Meta._orm_relationship_manager.resolve_relation_join( alias = model_cls.Meta.alias_manager.resolve_relation_join(
join_params.from_table, to_table join_params.from_table, to_table
) )
if alias not in self.used_aliases: if alias not in self.used_aliases:

View File

@ -1,5 +1,4 @@
import string import string
import string
import uuid import uuid
from enum import Enum from enum import Enum
from random import choices from random import choices