在flask中,我们想要生成的model如下:
# coding: utf-8
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Clientset(db.Model):
__tablename__ = 'clientset'
id = db.Column(db.Integer, primary_key=True)
compid = db.Column(db.Integer, info=' 企业ID')
n = db.Column(db.String(20, 'utf8mb4_0900_ai_ci'), info='名称')
v = db.Column(db.Text(collation='utf8mb4_0900_ai_ci'), info=' 内容')
i = db.Column(db.Integer, info='整数内容')
可有时生成的确是:
# coding: utf-8
from sqlalchemy import Column, Date, DateTime, Float, Index, String, Text
from sqlalchemy.dialects.mysql import BIGINT, BIT, INTEGER, TEXT, TINYINT, VARCHAR
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class Clientset(Base):
__tablename__ = 'clientset'
__table_args__ = {'comment': '服务器企业设置'}
id = Column(INTEGER(11), primary_key=True)
compid = Column(INTEGER(11), comment=' 企业ID')
n = Column(VARCHAR(20), comment='名称')
v = Column(TEXT, comment=' 内容')
i = Column(INTEGER(11), comment='整数内容')
经过一个下午的折腾,终于弄好了。记录一下:上面第二种基于Base的样式是sqlacodegen生成的,第一种才是flask应有的样式。安装包是注意不要安装 sqlacodegen。 只安装flask-sqlacodegen.
同时生成命令如下:
python -m sqlacodegen.main --flask --outfile models.py mysql+pymysql://<username>:<password>@<database-ip>:<port>/<database-name> [--tables <tablenames>] [--notables]