On May 23, 2013, at 1:29 AM, Anthony Kong <[email protected]> wrote:

> Hi, all,
> 
> We are upgrading our application to use SA0.8.0. For reason outside our 
> control, some XP workstations are using old sybase ODBC driver and we cannot 
> upgrade them as of yet. 
> 
> The sybase driver will cause this problem in the following situation:
> 
> 1) we have a table that have a field of type 'bit' to hold boolean value 
> (i.e. 0 for false and 1 for true)
> 
> 2) we need to select subset data out of this table based on this flag
> 
> When we run something like 
> 
>     session.query(SubProduct).filter(Product.isTradeable==True)
> 
> we get this error message:
> 
> DatabaseError: (DatabaseError) Msg 257, Level 16, State 1, Line 1
> Implicit conversion from datatype 'CHAR' to 'BIT' is not allowed.  Use the 
> CONVERT function to run this query.
> Msg 257, Level 16, State 1, Line 1
> Implicit conversion from datatype 'CHAR' to 'BIT' is not allowed.  Use the 
> CONVERT function to run this query.
>  'SELECT SUBPRODUCT ... WHERE Product.id_tradeable = @id_tradeable_1' 
> {'@id_tradeable_1': '1'}
> 
> I am trying to put in place a workaround: If I can convert the sql statement 
> to something like so
> 
>  'SELECT SUBPRODUCT ... WHERE Product.id_tradeable = convert(bit, 
> @id_tradeable_1)' {'@id_tradeable_1': '1'}
> 
> 
> I believe I can avoid the sybase SQL exception regardless of client driver 
> version
> 
> So I implemented this class
> 
> 
> import sqlalchemy.types as satypes
> from sqlalchemy import func
> from sqlalchemy.sql.expression import type_coerce
> 
> class SybaseBit( satypes.TypeDecorator):
>     impl = satypes.Boolean
> 
>     def bind_expression(self, bindvalue):
>         # bindvalue = type_coerce(bindvalue, satypes.Integer)
>         return func.convert(bit, bindvalue)
>         
> It failed because 'bit' is not defined there. If I put in a string 'bit', the 
> generated sql is very close to what I want.
> 
> So my question is:
> 
> What is the proper way to implement this?

you can get the string "bit" in there using sqlalchemy.literal_column('bit').


-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to