Skip to content

Optimize joins in search and sorting, add related tests#903

Open
pokidovea-playrix wants to merge 3 commits intosmithyhq:mainfrom
pokidovea-playrix:bugfix/search-by-multiple-related-columns
Open

Optimize joins in search and sorting, add related tests#903
pokidovea-playrix wants to merge 3 commits intosmithyhq:mainfrom
pokidovea-playrix:bugfix/search-by-multiple-related-columns

Conversation

@pokidovea-playrix
Copy link
Copy Markdown

Implemented logic to avoid redundant table joins in search and sorting by tracking joined tables. Added tests to validate searching by multiple related columns to ensure correctness and robustness. Updated .gitignore to include .idea/.
Related issue #901

Implemented logic to avoid redundant table joins in search and sorting by tracking joined tables. Added tests to validate searching by multiple related columns to ensure correctness and robustness. Updated `.gitignore` to include `.idea/`.
Replaced direct `__tablename__` access with `class_mapper(model).mapped_table.name` for better compatibility and reliability.
Comment thread sqladmin/models.py

@staticmethod
def _get_joined_tables(stmt: Select) -> set[str]:
return {table.name for table, *_ in stmt._setup_joins}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi @pokidovea-playrix , thanks for your contribution.

I think stmt._setup_joins is not recommended because it is a private (internal) attribute of SQLAlchemy. Code may break if SQLAlchemy changes this in the future.

Comment thread sqladmin/models.py
return pairs

@staticmethod
def _get_joined_tables(stmt: Select) -> set[str]:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As the package should be compatible with Python 3.8, please use Set[str] instead of set[str].

)


class Table3(Base):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Model names could be more descriptive and intuitive, clearly reflecting a real-world relations. For example : Continent, Country, City

def base_content():
with SessionLocal() as session:
# Add base data to the database
t1 = Table1(id=1, field1="Field1_Value1")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Variable naming can be more descriptive

yield c


def base_content():
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Isn't better to make it a fixture?

) -> None:
base_content()
response = client.get(f"/admin/table3/list?search={term}")
assert response.status_code == 200
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It would be nice if test follows Arrange-Act-Assert pattern.

@mmzeynalli mmzeynalli linked an issue Apr 5, 2026 that may be closed by this pull request
2 tasks
@mmzeynalli mmzeynalli added the waiting-for-feedback Waiting feedback/answer/updates from contributor label Apr 7, 2026
@mmzeynalli
Copy link
Copy Markdown
Member

@pokidovea any updates? This is the same PR as #983. I have to merge one of these, I will go with the one that is approved first. Will close second due to duplication.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting-for-feedback Waiting feedback/answer/updates from contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error while searching by multiple columns of joined model

5 participants