On Thu, Aug 15, 2013 at 12:21:43PM -0400, Michael Bayer wrote:
> 
> On Aug 14, 2013, at 9:24 PM, Jeff Dairiki <dair...@dairiki.org> 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 .

Thank you for the quick response (as always), Mike.

But I'm still fuddled.

Okay, so I can customize how __invert__(), __and__() and __or__() (all
the operations that evaluate their arguments in a boolean context) get
compiled.  (I think that's what you're pointing me at, right?)
(Really cool, by the way!)

But that still won't "fix" my original example where .filter(MyTable.ynbool)
produces "SELECT ... WHERE MyTable.ynbool;", since none of those operations
are involved.

I've tried adding a custom comparator_factory.__nonzero__() operator
to my type, but that didn't seem to work.  (Though it's possible I did
it wrong.)  Should it have?

Anyhow, this is not even close to the highest priority item in my
queue, ATM.  (So it shouldn't be for you either, unless there's an
easy solution that I'm not seeing.)  I will look into it further at
some point in the future.

Jeff



-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to