插入时sqlalchemy utf8错误

时间:2016-06-09 13:55:27

标签: mysql python-2.7 utf-8 sqlalchemy

使用sql alchemy ORM插入MySQL数据库时出现UTF-8错误。当我使用原始sql运行时,相同的查询运行正常。这是ORM查询:

rotating_keys_object = rotating_keys_model(
            gcm_key=rot_gcm_key,
            nonce=rot_nonce,
            tag=rot_tag,
            operational_team_id=self.operational_team_id
        )
session.add(rotating_keys_object)

这是错误:

DatabaseError: (raised as a result of Query-invoked autoflush;
  consider using a session.no_autoflush block if this flush is occurring
  prematurely) (DatabaseError) 1300: 
Invalid utf8 character string: 'FE4587'
  u'INSERT INTO rotating_keys (gcm_key, nonce, tag, operational_team_id)
       VALUES (%(gcm_key)s, %(nonce)s, %(tag)s, %(operational_team_id)s)'
       {'gcm_key': '\xfeE\x87\xe7\xc9\xe5\xec\xe0\x9c\xd6\x85\x11\xc7\xebd\xe3\x7f\xd9\xfel\xe6\x86"j\xbe=\xf4\xd7\x95\x99F\x8f',
        'nonce': 'o\xcb\x06\xe0\xe9\xech\xed\xed?T\xf4\xaf\x9a\xe1N',
        'operational_team_id': 1,
        'tag': ";p\xcce\xd2\xb8'\xf5\x89q\xc1\xa0\xfa\xff\x11\xf9"}

0 个答案:

没有答案