Skip to content
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/query_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def get_joins_for_related_field(
related_table: Table = related_field.related_model._meta.basetable
if isinstance(related_field, ManyToManyFieldInstance):
through_table = Table(related_field.through)
related_table = related_table.as_(f"{table.get_table_name()}__{related_field_name}")
required_joins.append(
(
through_table,
Expand Down Expand Up @@ -124,6 +125,10 @@ def resolve_nested_field(
related_table = related_table.as_(
f"{table.get_table_name()}__{iter_field.model_field_name}"
)
if isinstance(related_field, ManyToManyFieldInstance):
related_table = related_table.as_(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code duplicate the code of the check above. You can rewrite it as

if isinstance(related_field, (ForeignKeyFieldInstance, ManyToManyFieldInstance)):

f"{table.get_table_name()}__{iter_field.model_field_name}"
)
table = related_table

last_field = fields[-1]
Expand All @@ -141,6 +146,10 @@ def resolve_nested_field(
related_table = related_table.as_(
f"{table.get_table_name()}__{related_field.model_field_name}"
)
if isinstance(related_field, ManyToManyFieldInstance):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elif should prevent a redundant check.

related_table = related_table.as_(
f"{table.get_table_name()}__{related_field.model_field_name}"
)

term = related_table[related_field_meta.db_pk_column]
else:
Expand Down