python flask-sqlalchemy如何设置使自动建的mysql表字符集charset为utf8

问题

发现flask-sqlalchemy自动创建的mysql表为默认的latin1,如何不更改mysql服务器的默认字符集,直接在flask里配置自动建的mysql表字符集为utf8?

最佳答案

配置table_args就可以了,如:

class Foo(Base):
    __tablename__ = "foo"
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}

    ...

    column = db.Column(db.String(500))
标签:SQLAlchemyFlaskPythonMySQL 发布于:2019-11-12 04:30:27