On Aug 14, 2013, at 9:24 PM, Jeff Dairiki <[email protected]> wrote:

> I'm working with an existing MySQL schema that has lots of columns of
> type ENUM('N', 'Y').  I'd like to deal with them as real booleans on
> the python side.
> 
> I have a simple TypeDecorator which almost works (I think):
> 
>    class YNBoolean(sqlalchemy.types.TypeDecorator):
> 
>        impl = mysql.ENUM('N', 'Y', charset='ascii')
> 
>        def process_bind_param(self, value, dialect):
>            if value is None:
>                return value
>            return 'Y' if value else 'N'
> 
>        def process_result_value(self, value, dialect):
>            if value is None:
>                return None
>            return value == 'Y'
> 
> The one problem I've discovered with this is that
> 
>    session.query(MyTable).filter(MyTable.ynbool)
> 
> produces a query like
> 
>    SELECT ... FROM MyTable WHERE MyTable.ynbool;
> 
> What I really want is
> 
>    SELECT ... FROM MyTable WHERE MyTable.ynbool = 'Y';
> 
> 
> (If I do .filter(MyTable.ynbool == True) that does work as desired/expected.)
> 
> 
> Is there a way to customize how my column gets compiled when used
> in an expression in a boolean context?  (If not, I can live with it
> as is.  I'll just get surprised once in awhile when I forget that
> treating the column as a boolean in expressions won't work.)
> 

there is now, check out 
http://docs.sqlalchemy.org/en/rel_0_8/core/types.html#types-operators .



Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to