Skip to content

feat: pass model to apply#65

Open
ArzelaAscoIi wants to merge 1 commit into
gorilla-co:masterfrom
ArzelaAscoIi:feat/passModelToApply
Open

feat: pass model to apply#65
ArzelaAscoIi wants to merge 1 commit into
gorilla-co:masterfrom
ArzelaAscoIi:feat/passModelToApply

Conversation

@ArzelaAscoIi
Copy link
Copy Markdown

@ArzelaAscoIi ArzelaAscoIi commented Feb 20, 2025

Add support for explicitly passing a model to apply_odata_query

Summary

This PR introduces an enhancement to the apply_odata_query function in odata_query/sqlalchemy/shorthand.py, allowing users to explicitly pass a model when applying filters. This addresses cases where the query does not inherently provide enough context for determining the correct model, avoiding errors when filtering on fields that exist only in specific tables.

Problem

Previously, calling apply_odata_query on a query involving multiple joined tables could lead to an InvalidFieldException if the function attempted to resolve a field that only exists in a specific table. For example, in the following query:

SELECT DISTINCT B.name, B.tag_id 
FROM A 
JOIN A_B_MAPPING 
ON A.id = A_B_MAPPING.id 
JOIN B ON B.id = A_B_MAPPING.id

Filtering on A.i_only_exist_in_table_A with:

odata_query.sqlalchemy.apply_odata_query(query, "i_only_exist_in_table_A eq 'something'")

would result in an error because apply_odata_query attempts to resolve the field from B, which does not have i_only_exist_in_table_A.

Solution

To address this, the PR introduces:

  • A new helper function _get_model that encapsulates logic for determining the model, either from the query itself or from an explicitly provided model.
  • Updates to apply_odata_query to accept an optional model parameter, allowing users to specify the model explicitly when needed.

Changes

  • Enhancements to odata_query/sqlalchemy/shorthand.py:

    • Added imports for Type and DeclarativeMeta to support SQLAlchemy model type hinting.
    • Introduced _get_model to handle model resolution logic.
    • Modified apply_odata_query to leverage _get_model, enabling explicit model passing.
  • Testing improvements:

    • Added a new test case test_query_with_explicit_model to verify that filtering works correctly when a model is explicitly passed.
    • Ensured compatibility with both SQLAlchemy ORM 1.x and the current version.

Impact

This change provides greater flexibility and robustness in applying OData filters, particularly when working with complex queries involving multiple joined tables.

Let me know if you need any adjustments! 🚀

MrToCode added a commit to MrToCode/odata-query that referenced this pull request Feb 18, 2026
…pstream gorilla-co#65)

Adds optional `model` parameter to `apply_odata_query()` so callers can
supply the root model explicitly, avoiding `InvalidFieldException` on
complex join queries where inference from `columns_clause_froms[0]` is
ambiguous. Extracts `_get_model()` helper; default inference behavior is
unchanged.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
MrToCode added a commit to MrToCode/odata-query that referenced this pull request Feb 18, 2026
[TC intake] upstream gorilla-co#65 pass model — add optional model override to apply_odata_query
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.

Managing more than one entity in AstToSqlAlchemyClauseVisitor

1 participant