Skip to content

Conversation

sietr0s
Copy link

@sietr0s sietr0s commented Jun 24, 2025

Fix incorrect JOIN aliasing when filtering on multiple ManyToMany relations to the same table

Description

This PR fixes an issue where Tortoise ORM fails to properly alias JOINs when filtering on two different ManyToMany relations pointing to the same model. The problem occurred because get_joins_for_related_field function didn't apply table aliasing for ManyToManyFieldInstance cases.

The main change modifies the get_joins_for_related_field function to add proper table aliasing for ManyToMany relations by using the pattern {table_name}__{field_name} for the alias.

Motivation and Context

When making queries that filter on two different ManyToMany relations to the same model (through different through tables), Tortoise ORM was generating SQL without proper table aliasing, causing ambiguity in the JOINs. This made such queries impossible to execute correctly.

Fixes issue #1969

@henadzit
Copy link
Contributor

henadzit commented Jun 24, 2025

@sietr0s thank you for the contribution! Can you please add a test exposing the issue? This requirement was a part of the PR template :)

Update: there are failing tests.

Copy link

codspeed-hq bot commented Jun 24, 2025

CodSpeed Performance Report

Merging #1972 will not alter performance

Comparing sietr0s:patch-1 (0f9c85d) with develop (4b4e4a5)

Summary

✅ 16 untouched benchmarks

sietr0s and others added 2 commits June 26, 2025 10:43
@coveralls
Copy link

Pull Request Test Coverage Report for Build 15905581699

Details

  • 5 of 5 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.009%) to 89.639%

Totals Coverage Status
Change from base Build 15492666891: 0.009%
Covered Lines: 6676
Relevant Lines: 7266

💛 - Coveralls

Copy link
Contributor

@henadzit henadzit left a comment

Choose a reason for hiding this comment

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

Can you please add a test exposing the original issue? Meaning that the new test should fail without your changes. This is a requirement for a PR to be merged. Thanks!

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)):

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants