postgresql – 异常中的SQLAlchemy Unicode问题
我正在开发一个带有postgres / SQLAlchemy / Flask-Admin的Flask应用程序.但是,在Admin界面中,由于unicode(exc)引发UnicodeDecodeError,因此无法报告包含Unicode字母的任何DB错误.
我能够找到问题到 class StatementError(SQLAlchemyError): ... def __unicode__(self): return self.__str__() 并通过以下方式重现问题: class A(Base): __tablename__="a" id = Column(Integer,primary_key=True) name = Column(String) name2 = Column(String,nullable=False) session = Session() a = A(name=u"?????") session.add(a) try: session.commit() except Exception as e: print(repr(e)) print("------------------") print(unicode(e)) 哪个回报: ProgrammingError('(psycopg2.ProgrammingError) column "name" of relation "a" does not existnLINE 1: INSERT INTO a (name,name2) VALUES ('xd7xa2xd7x91xd7xa8xd7x99xd7xaa',NULL) RETURNING...n ^n',) ------------------ Traceback (most recent call last): File "test.py",line 27,in <module> print(unicode(e)) UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 118: ordinal not in range(128) 我目前通过用从utf-8解码的类替换相关的异常来解决它.但是,这是一个可怕的黑客攻击,我正在寻找一个合适的解决方案: >有没有办法配置SQLAlchemy自动解码收到的错误消息? (这个问题只与Python2有关.在Python3中,上面的代码可以工作.我相信这是因为默认编码是utf-8)
我实际上认为从您的应用程序修补SQLAlchemy是正确的合理清洁解决方案.原因如下:
>您已经确定了一些通常被认为是SQLAlchemy中的错误的内容. 这是一个简单的程序,可以修补sqlalchemy.exc.StatementError并测试补丁.如果你想要甚至可以尝试生成包括unicode在内的异常,请将其转换为unicode,并且只有在引发UnicodeDecodeError时才应用补丁.如果您这样做,当sqlalchemy修复问题时,您的补丁将自动停止应用. # -*- coding: utf-8 -*- from sqlalchemy.exc import StatementError def statement_error_unicode(self): return unicode(str(self),'utf-8') # See <link to sqlalchemy issue>; can be removed once we require a # version of sqlalchemy with a fix to that issue StatementError.__unicode__ = statement_error_unicode message = u'Sqlalchemy unicode |