Incorrect string value: ‘\xE6\xB5\x8B\xE8\xAF\x95’ for column ‘region_name’

insert语句加的该字段有汉字,乱码造成的

解决办法:

mysql建表结构的时候指定编码格式utf-8就可以解决了。

class User(Base):
    __tablename__ = 'user'
    __table_args__ = {
        'comment': '用户表',
        'mysql_collate': 'utf8_general_ci',
        'mysql_engine': 'InnoDB',
        'mysql_charset': 'utf8',
    }
    id = Column(INTEGER(11), primary_key=True)
    name = Column(VARCHAR(255), comment='用户名')
    account = Column(VARCHAR(255), comment='账号')
    passwd = Column(VARCHAR(255), comment='密码')
    phone = Column(VARCHAR(255), comment='联系方式')
    email = Column(VARCHAR(100), server_default=text("''"), comment='email')
    authority = Column(TINYINT(2), nullable=False, server_default=text("'0'"), comment='权限:0普通用户,1管理员')
    status = Column(TINYINT(2), nullable=False, server_default=text("'0'"), comment='启用:0开启,1关闭')
    notice = Column(TINYINT(2), server_default=text("'0'"), comment='通知:0开启,1关闭)')

关键代码

__table_args__ = {
        'comment': '用户表',
        'mysql_collate': 'utf8_general_ci',
        'mysql_engine': 'InnoDB',
        'mysql_charset': 'utf8',
    }