feat: pass model to apply#65
Open
ArzelaAscoIi wants to merge 1 commit into
Open
Conversation
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
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.
Add support for explicitly passing a model to
apply_odata_queryAstToSqlAlchemyClauseVisitor#24Summary
This PR introduces an enhancement to the
apply_odata_queryfunction inodata_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_queryon a query involving multiple joined tables could lead to anInvalidFieldExceptionif the function attempted to resolve a field that only exists in a specific table. For example, in the following query:Filtering on
A.i_only_exist_in_table_Awith:would result in an error because
apply_odata_queryattempts to resolve the field fromB, which does not havei_only_exist_in_table_A.Solution
To address this, the PR introduces:
_get_modelthat encapsulates logic for determining the model, either from the query itself or from an explicitly provided model.apply_odata_queryto accept an optionalmodelparameter, allowing users to specify the model explicitly when needed.Changes
Enhancements to
odata_query/sqlalchemy/shorthand.py:TypeandDeclarativeMetato support SQLAlchemy model type hinting._get_modelto handle model resolution logic.apply_odata_queryto leverage_get_model, enabling explicit model passing.Testing improvements:
test_query_with_explicit_modelto verify that filtering works correctly when a model is explicitly passed.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! 🚀