Open
Conversation
Contributor
|
@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. |
CodSpeed Performance ReportMerging #1972 will not alter performanceComparing Summary
|
Fix problems alias
Pull Request Test Coverage Report for Build 15905581699Details
💛 - Coveralls |
henadzit
reviewed
Jun 27, 2025
Contributor
henadzit
left a comment
There was a problem hiding this comment.
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_( |
Contributor
There was a problem hiding this comment.
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): |
Contributor
There was a problem hiding this comment.
elif should prevent a redundant check.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_fieldfunction didn't apply table aliasing for ManyToManyFieldInstance cases.The main change modifies the
get_joins_for_related_fieldfunction 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