我正在使用postgres/SQLAlchemy/Flask-Admin处理Flask应用程序。但是,在Admin界面中,由于unicode(exc)
会产生UnicodeDecodeError
,因此无法报告包含Unicode字母的任何数据库错误。SQLAlchemy在异常中的Unicode问题
我能够一起找到问题sqlalchemy.exc
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 exist\nLINE 1: INSERT INTO a (name, name2) VALUES (\'\xd7\xa2\xd7\x91\xd7\xa8\xd7\x99\xd7\xaa\', 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)
我目前解决这个问题用我的从解码的类取代相关的例外。但是,这是一个可怕的黑客,我正在寻找一个适当的解决方案:
- 有没有办法配置SQLAlchemy自动解码接收到的错误消息?
- 有没有办法在
latin
编码(不太有利,但accetable) - 有没有办法让
unicode
尝试utf-8
,而不是ascii
/latin
解码配置Postgres的输出信息? - 有什么办法可以解决它?
(问题是相关的只有Python2在Python3上述工程的代码。我相信这是因为默认的编码是utf-8
)
我所说的,在SQLAlchemy的TBH一个bug,'__str__'应该返回一个'str'和'__unicode__'应该返回一个'unicode'。您应该将此作为问题提交。你可以通过在所有地方执行print(str(e).decode(“utf-8”))来解决它,这很乏味,但不是世界上最糟糕的事情。 – univerio
我会将此作为一个问题。但是我无法按照建议修复它,因为相关代码位于Flask-Admin中。我需要一个更全球化的解决方案。 – tmrlvi
@tmrlvi您可以在这里发帖链接到您提交的问题吗? –