python - SQLAlchemy mapping table with non-ascii columns to class -
item = table('item', metadata, autoload=true, autoload_with=engine, encoding = 'cp1257') class item(object): pass sqlalchemy.orm import mapper mapper(item, item)
i error:
line 43, in <module> mapper(item, item) file "c:\python27\lib\site-packages\sqlalchemy\orm\__init__.py", line 890, in mapper return mapper(class_, local_table, *args, **params) file "c:\python27\lib\site-packages\sqlalchemy\orm\mapper.py", line 211, in __init__ self._configure_properties() file "c:\python27\lib\site-packages\sqlalchemy\orm\mapper.py", line 578, in _configure_properties setparent=true) file "c:\python27\lib\site-packages\sqlalchemy\orm\mapper.py", line 618, in _configure_property self._log("_configure_property(%s, %s)", key, prop.__class__.__name__) file "c:\python27\lib\site-packages\sqlalchemy\orm\mapper.py", line 877, in _log (self.non_primary , "|non-primary" or "") + ") " + file "c:\python27\lib\site-packages\sqlalchemy\util.py", line 1510, in __get__ obj.__dict__[self.__name__] = result = self.fget(obj) file "c:\python27\lib\site-packages\sqlalchemy\sql\expression.py", line 3544, in description return self.name.encode('ascii', 'backslashreplace') unicodedecodeerror: 'ascii' codec can't decode byte 0xeb in position 7: ordinal not in range(128)
i connecting mssql. table autoload seems work. error while trying map. thank help!
mapping table class creates mapped properties on class. properties have same name of columns, default. since python 2.x allows ascii identifiers, fails if have non-ascii column names.
the solution can think of give identifiers different name when mapping table class.
the example below that. note i'm creating table on code simplicity, can run code without having existing table. same reflected table.
#-*- coding:utf-8 -*- import sqlalchemy sa import sqlalchemy.orm engine = sa.create_engine('sqlite://', echo=true) # new memory-only database metadata = sa.metadata(bind=engine) # create table. reflected database instead: tb = sa.table('foo', metadata, sa.column(u'id', sa.integer, primary_key=true), sa.column(u'nomé', sa.unicode(100)), sa.column(u'ãéìöû', sa.unicode(100)) ) tb.create() class foo(object): pass # maps table class, defining different property names # columns: sa.orm.mapper(foo, tb, properties={ 'nome': tb.c[u'nomé'], 'aeiou': tb.c[u'ãéìöû'] })
after can use foo.nome
refer nomé
column , foo.aeiou
refer ãéìöû
column.
Comments
Post a Comment