Issues

Issue #3143 closed

UnicodeEncodeError in StatementError

kodemi
created an issue

My models contains unicode fields. From time to time I get this error when quering records:

File "/usr/local/services/app/flights_handler.py", line 99, in fetch_flights_from_db
    flights = self.session.query(Flight).all()
  File "/home/fidsadm/.virtualenvs/vipport-services/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2286, in all
    return list(self)
  File "/home/fidsadm/.virtualenvs/vipport-services/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2398, in __iter__
    return self._execute_and_instances(context)
  File "/home/fidsadm/.virtualenvs/vipport-services/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2411, in _execute_and_instances
    close_with_result=True)
  File "/home/fidsadm/.virtualenvs/vipport-services/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2402, in _connection_from_session
    **kw)
  File "/home/fidsadm/.virtualenvs/vipport-services/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 843, in connection
    close_with_result=close_with_result)
  File "/home/fidsadm/.virtualenvs/vipport-services/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 847, in _connection_for_bind
    return self.transaction._connection_for_bind(engine)
  File "/home/fidsadm/.virtualenvs/vipport-services/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 298, in _connection_for_bind
    self._assert_active()
  File "/home/fidsadm/.virtualenvs/vipport-services/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 210, in _assert_active
    % self._rollback_exception
  File "/home/fidsadm/.virtualenvs/vipport-services/local/lib/python2.7/site-packages/sqlalchemy/exc.py", line 242, in __str__
    repr(self.statement), repr(params_repr)
  File "/home/fidsadm/.virtualenvs/vipport-services/local/lib/python2.7/site-packages/sqlalchemy/sql/util.py", line 254, in __repr__
    return repr(self.params)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-8: ordinal not in range(128)

Comments (5)

  1. Mike Bayer repo owner

    not able to reproduce. please modify the below to illustrate this stack trace:

    #!coding: utf-8
    
    from sqlalchemy.util import ue, u
    from sqlalchemy import exc
    
    unicode_param = u('méil')
    unicode_str = ue('\u6e2c\u8a66')
    
    exception = exc.StatementError(
        "some error", "some statement", {unicode_param: unicode_str}, Exception())
    
    msg = "original exception was: %s" % exception
    
    assert str(msg)
    
  2. Kirill Ivantsov

    The repr can't work with unicode. Update your model definitions to work with unicode. Do it like this

    class Foo(Base):
       __tablename__ = 'foo'
    
        def __unicode__(self):
            return self.name
        id = db.Column(db.Integer, primary_key=True)
        name = db.Column(db.Unicode(100), nullable=False)
            ....
    

    SQLAlchemy have Unicode and UnicodeText types. But i think, problem in your models, when you querying a model, interpeter trying to use repr for object representation like <object at 0xDEADBEEF>.

  3. Log in to comment