Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix incorrect use of pk_attr instead of db_pk_column when using RelationalField as the primary key #1362

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tortoise/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from tortoise.fields.relational import (
BackwardFKRelation,
BackwardOneToOneRelation,
RelationalField,
ForeignKeyFieldInstance,
ManyToManyFieldInstance,
ManyToManyRelation,
Expand Down Expand Up @@ -276,6 +277,12 @@ def add_field(self, name: str, value: Field) -> None:
field_filters = get_filters_for_field(
field_name=name, field=value, source_field=value.source_field or name
)
if value.pk:
field_filters.update(
get_filters_for_field(
field_name="pk", field=value, source_field=value.source_field or name
)
)
self._filters.update(field_filters)
self.finalise_fields()

Expand Down Expand Up @@ -618,6 +625,8 @@ def __search_for_field_attributes(base: Type, attrs: dict) -> None:
meta.pk = fields_map.get(pk_attr) # type: ignore
if meta.pk:
meta.db_pk_column = meta.pk.source_field or meta.pk_attr
if isinstance(meta.pk, RelationalField) and meta.pk.source_field is None:
meta.db_pk_column = f"{meta.db_pk_column}_id"
meta._inited = False
if not fields_map:
meta.abstract = True
Expand Down