On 2014-11-29 16:19, Michael Bayer wrote:
>>> opList = dbSession.query(PendingOperation).filter_by(opcode=opcode).all()
> 
> looks easy enough to reproduce:
> 
>     import codecs
> 
>     x = u'\u0142'
> 
>     codecs.utf_8_decode(x)
> 
> that is, in Py2K when you call decode() on a unicode object already, things 
> go wrong.
> 
> SQLAlchemy shouldn’t be attempting to run this decode operation unless the 
> MySQL driver used here is acting in a flaky way, that is, SQLAlchemy did a 
> test on first connect to see if a Unicode() type comes back as u’’ or not.   
> That it’s trying to do the decode means that it didn’t, yet then on this 
> column it is.
> 
> The MySQL driver in use here would be of top importance in determining why 
> this might be happening, I guess this is the “gaerdbms” driver?

Exactly! The connection string looks like this:

> 'mysql+gaerdbms:///{}?instance={}'.format(...)

when running on Google App Engine, and like this:

> 'mysql://{}:{}@{}/{}'.format(...)

when running in a local setting.

The DB session is created like this (upon connecting, we set SQL mode to
traditional):

> from sqlalchemy import create_engine, event
> from sqlalchemy.orm import sessionmaker
> 
> def makeEngine():
>     connectionString = ...
> 
>     eng = create_engine(connectionString,
>                 case_sensitive=True,
>                 encoding='utf-8',
>                 isolation_level='REPEATABLE READ'
>     )
> 
>     @event.listens_for(eng, "first_connect", insert=True) # make sure we're 
> the very first thing
>     @event.listens_for(eng, "connect")
>     def receive_connect(dbapi_connection, connection_record):
>         sqlMode = 'TRADITIONAL'
>         cursor = dbapi_connection.cursor()
>         cursor.execute("SET sql_mode = '{0}'".format(sqlMode))
> 
>     return eng
> 
> engine = makeEngine()
> Session = sessionmaker(
>             bind=engine,
>             autoflush=True,
>             autocommit=False
> )

> So the best approach for MySQL is to ensure that the driver is predictable 
> about unicode include, specify use_unicode=0 in the URL (see 
> http://docs.sqlalchemy.org/en/rel_0_9/dialects/mysql.html#unicode).

Like this? :

'mysql+gaerdbms:///{}?instance={}&charset=utf8&use_unicode=0'.format(...)

'mysql://{}:{}@{}/{}&charset=utf8&use_unicode=0'.format(...)

Why is gaerdbms flaky?
I understand this is a bug in the driver?
I don't think we have meddled with anything here, like MySQL's default
character encoding or something like that.

-- 
http://people.eisenbits.com/~stf/
http://www.eisenbits.com/

OpenPGP: 80FC 1824 2EA4 9223 A986  DB4E 934E FEA0 F492 A63B

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to