0

I have encountered an error while trying to automap tables from a MySQL databases. The error is :

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 7: ordinal not in range(128)

This error occurs when I am making a :

Base.prepare(engine, reflect=True)

My database is in utf8_bin.

Here is my engine connection to the database :

engine = create_engine("mysql://User:[email protected]:3308/db?charset=utf8", encoding="utf-8", echo=True)

And here is the complete stack trace of the error :

 2016年05月12日 15:57:39,497 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'
 2016年05月12日 15:57:39,497 INFO sqlalchemy.engine.base.Engine ()
 2016年05月12日 15:57:39,511 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()
 2016年05月12日 15:57:39,511 INFO sqlalchemy.engine.base.Engine ()
 2016年05月12日 15:57:39,537 INFO sqlalchemy.engine.base.Engine show collation where `Charset` = 'utf8' and `Collation` = 'utf8_bin'
 2016年05月12日 15:57:39,538 INFO sqlalchemy.engine.base.Engine ()
 2016年05月12日 15:57:39,552 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CHAR(60)) AS anon_1
 2016年05月12日 15:57:39,552 INFO sqlalchemy.engine.base.Engine ()
 2016年05月12日 15:57:39,566 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS CHAR(60)) AS anon_1
 2016年05月12日 15:57:39,566 INFO sqlalchemy.engine.base.Engine ()
 2016年05月12日 15:57:39,580 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin AS anon_1
 2016年05月12日 15:57:39,580 INFO sqlalchemy.engine.base.Engine ()
 2016年05月12日 15:57:39,620 INFO sqlalchemy.engine.base.Engine SHOW FULL TABLES FROM `GesCo`
 2016年05月12日 15:57:39,620 INFO sqlalchemy.engine.base.Engine ()
 2016年05月12日 15:57:39,652 INFO sqlalchemy.engine.base.Engine SHOW CREATE TABLE `Client`
 2016年05月12日 15:57:39,652 INFO sqlalchemy.engine.base.Engine ()
 2016年05月12日 15:57:39,670 INFO sqlalchemy.engine.base.Engine SHOW CREATE TABLE `ClientContact`
 2016年05月12日 15:57:39,670 INFO sqlalchemy.engine.base.Engine ()
 2016年05月12日 15:57:39,686 INFO sqlalchemy.engine.base.Engine SHOW CREATE TABLE `Dossier`
 2016年05月12日 15:57:39,686 INFO sqlalchemy.engine.base.Engine ()
 2016年05月12日 15:57:39,705 INFO sqlalchemy.engine.base.Engine SHOW CREATE TABLE `RDV`
 2016年05月12日 15:57:39,705 INFO sqlalchemy.engine.base.Engine ()
 Traceback (most recent call last):
 File "./api.py", line 29, in <module>
 Base.prepare(engine, reflect=True)
 File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/automap.py", line 788, in prepare
 map_config.map()
 File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/declarative/base.py", line 592, in map
 return super(_DeferredMapperConfig, self).map()
 File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/declarative/base.py", line 529, in map
 **self.mapper_args
 File "<string>", line 2, in mapper
 File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 627, in __init__
 self._configure_properties()
 File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 1318, in _configure_properties
 setparent=True)
 File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 1607, in _configure_property
 prop.instrument_class(self)
 File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/properties.py", line 182, in instrument_class
 doc=self.doc
 File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/attributes.py", line 1489, in register_descriptor
 manager.instrument_attribute(key, descriptor)
 File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/instrumentation.py", line 215, in instrument_attribute
 self.install_descriptor(key, inst)
 File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/instrumentation.py", line 269, in install_descriptor
 setattr(self.class_, key, inst)
 UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 7: ordinal not in range(128)`

Thanks in advance for any help you can give me

EDIT:

Changed all charset variables and the collations variables in mysql, and it stills produces the same error :

mysql> show variables like 'collation%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
mysql> show variables like 'character\_set\_%';
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
+--------------------------+--------+
asked May 12, 2016 at 14:19

2 Answers 2

1

Did you try setting MySQL variables like character_set_client to utf-8? You might want to run the following query in MySQL and set all values to utf-8:

mysql> show variables like 'character\_set\_%';

You can also try setting some of the utf-8 collations like utf8_general_ci to variables in output of:

mysql> show variables like 'collation%';

answered May 12, 2016 at 17:05
Sign up to request clarification or add additional context in comments.

1 Comment

Edited the question, still no changes
0

It looks like you have a column name that has a é in it, and SQLAlchemy is trying to use that as the attribute name of your model class, but Python 2 only allows ASCII attribute names.

You'll need to explicitly define this particular class with the changed name:

Base = automap_base()
class RDV(Base):
 __tablename__ = "RDV"
 ...
 accepted = Column("accepté", Boolean, ...)
answered May 12, 2016 at 20:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.