refactors in foreign key
This commit is contained in:
@ -6,13 +6,13 @@ from orm.fields import (
|
|||||||
DateTime,
|
DateTime,
|
||||||
Decimal,
|
Decimal,
|
||||||
Float,
|
Float,
|
||||||
|
ForeignKey,
|
||||||
Integer,
|
Integer,
|
||||||
JSON,
|
JSON,
|
||||||
String,
|
String,
|
||||||
Text,
|
Text,
|
||||||
Time,
|
Time,
|
||||||
)
|
)
|
||||||
from orm.fields.foreign_key import ForeignKey
|
|
||||||
from orm.models import Model
|
from orm.models import Model
|
||||||
|
|
||||||
__version__ = "0.0.1"
|
__version__ = "0.0.1"
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
from typing import Any, List, Optional, TYPE_CHECKING, Type, Union
|
from typing import Any, List, Optional, TYPE_CHECKING, Tuple, Type, Union
|
||||||
|
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
@ -49,6 +49,28 @@ class ForeignKey(BaseField):
|
|||||||
to_column = self.to.__model_fields__[self.to.__pkname__]
|
to_column = self.to.__model_fields__[self.to.__pkname__]
|
||||||
return to_column.get_column_type()
|
return to_column.get_column_type()
|
||||||
|
|
||||||
|
def extract_model_from_sequence(
|
||||||
|
self, value: Any, child: "Model"
|
||||||
|
) -> Tuple[Union["Model", List["Model"]], bool]:
|
||||||
|
if isinstance(value, list) and not isinstance(value, self.to):
|
||||||
|
model = [self.expand_relationship(val, child) for val in value]
|
||||||
|
return model, True
|
||||||
|
|
||||||
|
if isinstance(value, self.to):
|
||||||
|
model = value
|
||||||
|
else:
|
||||||
|
model = self.to(**value)
|
||||||
|
return model, False
|
||||||
|
|
||||||
|
def construct_model_from_pk(self, value: Any) -> "Model":
|
||||||
|
if not isinstance(value, self.to.pk_type()):
|
||||||
|
raise RelationshipInstanceError(
|
||||||
|
f"Relationship error - ForeignKey {self.to.__name__} "
|
||||||
|
f"is of type {self.to.pk_type()} "
|
||||||
|
f"while {type(value)} passed as a parameter."
|
||||||
|
)
|
||||||
|
return create_dummy_instance(fk=self.to, pk=value)
|
||||||
|
|
||||||
def expand_relationship(
|
def expand_relationship(
|
||||||
self, value: Any, child: "Model"
|
self, value: Any, child: "Model"
|
||||||
) -> Optional[Union["Model", List["Model"]]]:
|
) -> Optional[Union["Model", List["Model"]]]:
|
||||||
@ -56,31 +78,22 @@ class ForeignKey(BaseField):
|
|||||||
if value is None:
|
if value is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
is_sequence = False
|
||||||
|
|
||||||
if isinstance(value, orm.models.Model) and not isinstance(value, self.to):
|
if isinstance(value, orm.models.Model) and not isinstance(value, self.to):
|
||||||
raise RelationshipInstanceError(
|
raise RelationshipInstanceError(
|
||||||
f"Relationship error - expecting: {self.to.__name__}, "
|
f"Relationship error - expecting: {self.to.__name__}, "
|
||||||
f"but {value.__class__.__name__} encountered."
|
f"but {value.__class__.__name__} encountered."
|
||||||
)
|
)
|
||||||
|
|
||||||
if isinstance(value, list) and not isinstance(value, self.to):
|
if isinstance(value, (dict, list, self.to)):
|
||||||
model = [self.expand_relationship(val, child) for val in value]
|
model, is_sequence = self.extract_model_from_sequence(value, child)
|
||||||
return model
|
|
||||||
|
|
||||||
if isinstance(value, self.to):
|
|
||||||
model = value
|
|
||||||
elif isinstance(value, dict):
|
|
||||||
model = self.to(**value)
|
|
||||||
else:
|
else:
|
||||||
if not isinstance(value, self.to.pk_type()):
|
model = self.construct_model_from_pk(value)
|
||||||
raise RelationshipInstanceError(
|
|
||||||
f"Relationship error - ForeignKey {self.to.__name__} "
|
|
||||||
f"is of type {self.to.pk_type()} "
|
|
||||||
f"while {type(value)} passed as a parameter."
|
|
||||||
)
|
|
||||||
model = create_dummy_instance(fk=self.to, pk=value)
|
|
||||||
|
|
||||||
|
if not is_sequence:
|
||||||
model._orm_relationship_manager.add_relation(
|
model._orm_relationship_manager.add_relation(
|
||||||
model, child, virtual=self.virtual,
|
model, child, virtual=self.virtual
|
||||||
)
|
)
|
||||||
|
|
||||||
return model
|
return model
|
||||||
|
|||||||
Reference in New Issue
Block a user