Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix cast failed in search field of VARCHAR2 in oracle #1971

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

pedia
Copy link

@pedia pedia commented Mar 27, 2020

A field in Oracle table declared as:

DESCRIPTION VARCHAR2(2048 BYTE)

Access db code like:

from sqlalchemy.dialects.oracle import VARCHAR2

description = Column(VARCHAR2(2048), nullable=True)

# add this in admin.py
column_searchable_list = ('description',)

exception caught in search:

sqlalchemy.exc.DatabaseError: (cx_Oracle.DatabaseError) ORA-00906: missing left parenthesis [SQL: SELECT count(:count_2) AS count_1 FROM t_event_log WHERE lower(CAST(t_event_log.description AS VARCHAR2)) LIKE lower(:param_1)] [parameters: {'count_2': '*', 'param_1': '%100003%'}] (Background on this error at: http://sqlalche.me/e/4xp6)

The SQL should be

CAST(t_event_log.description AS VARCHAR2(2048))

'(2048)' is missing in SQL which cause error.

This PR fixed this.

@pedia pedia changed the title fix cast failed VARCHAR2 in oracle fix cast failed in search field of VARCHAR2 in oracle Jun 8, 2020
@pedia
Copy link
Author

pedia commented Jun 8, 2020

@mrjoes Would you please review this PR?

@mrjoes
Copy link
Contributor

mrjoes commented Jun 8, 2020

Unfortunately, I can't merge this as-is, as it will break compatibility with non-string types that require forced conversion into a string for LIKE to work. This is a main reason why the CAST is made.

I'm not sure what's the right solution here for Oracle if Oracle always wants a length of a string in a string type.

@pedia
Copy link
Author

pedia commented Jun 9, 2020

@mrjoes

Take a look at the closed issue from sqlalchemy?

I think

cast(field, field.type)

is more reasonable than:

cast(field, String)

This PR only fixed error for:

column_searchable_list = ('description',)

and there is no more harmful for

column_searchable_list = ('an int field',)

@mrjoes
Copy link
Contributor

mrjoes commented Jun 9, 2020

Got it. I will have to run the test, because I doubt that LIKE would work when field is casted to an INT explicitly.

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

Successfully merging this pull request may close these issues.

None yet

4 participants