Using Python 2.7 and SQLAlchemy 0.7, I'm connecting to a MySQL DB with the command:
engine = create_engine('mysql://username:password@host/dbname?charset=utf8',echo=False)
According to the SQLAlchemy docs, setting charset=utf8 automatically implies use_unicode=1, so that all strings should come back as unicode. http://docs.sqlalchemy.org/en/rel_0_7/dialects/mysql.html specifically gives the example
#set client encoding to utf8; all strings come back as unicode create_engine('mysql+mysqldb:///mydb?charset=utf8')
So why, then, when I query a text field in a mapped class, does that field end up with type "str"?
Base = declarative_base(engine)
class RegionTranslation(Base):
''''''
__tablename__ = 'RegionTranslation'
__table_args__ = {'autoload':True}
def __init__(self, region_id, lang_id, name):
self.region_id = region_id
self.lang_id = lang_id
self.name = name
rtrans = session.query(RegionTranslation).filter_by(region_id = 1, lang_id = 6).one()
print (type(rtrans.name))
The output is
<type 'str'>
If I just accept this and decode the string before using it, things are fine. But I don't get why the above code isn't returning the type 'unicode'. Can someone please, please explain this?
Finally found the answer when discovering that a different script I'd run successfully many times was no longer working.
I had changed the collation in my database from utf8_general_ci to utf8_bin. There is a bug in MySQLdb 1.2.3 that causes utf8_bin strings not to be recognized as text, so the unicode conversion isn't happening. This was fixed in MySQLdb 1.2.4.
https://sourceforge.net/p/mysql-python/bugs/289/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With