progress in removing resolving_relation_name as it's wrong for multiple related columns leading to same model

This commit is contained in:
collerek
2020-12-21 12:03:59 +01:00
parent 2d74b7bd47
commit 514e8c4ad5
5 changed files with 32 additions and 10 deletions

View File

@ -240,7 +240,8 @@ def verify_constraint_names(
) -> None:
"""
Verifies if redefined fields that are overwritten in subclasses did not remove
any name of the column that is used in constraint as it will fail.
any name of the column that is used in constraint as it will fail in sqlalchemy
Table creation.
:param base_class: one of the parent classes
:type base_class: Model or model parent class

View File

@ -55,9 +55,11 @@ class ModelTableProxy:
@classmethod
def get_related_field_name(cls, target_field: Type["BaseField"]) -> str:
if issubclass(target_field, ormar.fields.ManyToManyField):
return cls.resolve_relation_name(target_field.through, cls)
return cls.resolve_relation_name(
target_field.through, cls, explicit_multi=True
)
if target_field.virtual:
return cls.resolve_relation_name(target_field.to, cls)
return target_field.related_name or cls.get_name() + "s"
return target_field.to.Meta.pkname
@staticmethod
@ -113,7 +115,7 @@ class ModelTableProxy:
target_field, ormar.fields.ManyToManyField
):
return self.pk
related_name = self.resolve_relation_name(self, target_field.to)
related_name = target_field.name
related_model = getattr(self, related_name)
return None if not related_model else related_model.pk