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.
Pull Request Description
This pull request addresses the issue of UUID handling in
odata-query
with SQLAlchemy, ensuring that UUIDs are treated correctly in query filters, which was previously causing errors. My solution is particularly relevant to the ongoing discussion in Issue #25 ("Filter not working for uuid column") raised by schwingkopf and others, where similar issues were encountered when filtering on UUID columns.Problem Statement:
In my case, I encountered an issue where filtering by UUID columns in a PostgreSQL database resulted in the following error:
This occurred because
odata-query
was generating SQL queries that incorrectly treated UUIDs asVARCHAR
types, leading to invalid SQL statements. The error message highlighted that theUUID
type was being compared with aVARCHAR
, which is not allowed.This issue mirrors the one reported in Issue #25, where filtering by UUID columns produced no results due to a similar type mismatch.
Changes Made:
grammar.py
Changes:_UUID_PATTERN
) to properly recognize UUIDs with or without quotes.GUID
method to remove unnecessary quotes and parse UUIDs asGUID
objects:This change ensures that UUIDs are treated correctly in the query parsing phase.
common.py
Changes:visit_GUID
method to ensure UUIDs are passed asUUID
objects in SQLAlchemy queries instead ofVARCHAR
strings:This is essential to prevent the type mismatch that was causing the errors. As mentioned in Issue Filter not working for uuid column #25, SQLAlchemy expects UUIDs to be handled in a specific way, and this change resolves the mismatch by ensuring that the UUID remains in its proper form throughout query generation.
Test Changes:
Updated tests in both
test_odata_to_sqlalchemy_core.py
andtest_odata_to_sqlalchemy_orm.py
:Updated UUID comparisons to use
UUID
objects:Updated
IN
clause tests:These changes ensure that the UUIDs are passed in their correct type during tests, which prevents the type mismatch errors that were mentioned in Issue Filter not working for uuid column #25 when handling UUIDs in
IN
clauses.Contribution to the Discussion:
This pull request contributes directly to the ongoing conversation in Issue #25, where both I and other users faced similar UUID-related problems. The problem, as described in the issue, revolves around handling UUIDs properly in queries, particularly ensuring they are treated as UUID types instead of strings or VARCHARs.
In my solution, I upgraded SQLAlchemy to version ^2.0, which offers better support for UUIDs. I have tested my changes with both PostgreSQL and SQLite databases, and in both cases, I was able to successfully filter records using UUID values.
Thank you for providing such a useful library, and I hope this PR helps to resolve the issue or at least opens further discussion on UUID handling.