flask_sqlalchemy
"""
SQLAlchemy常用数据类型
Integer:整形,映射到数据库中是int类型。
Float:浮点类型,映射到数据库中是float类型。他占据的32位。
Double:双精度浮点类型,映射到数据库中是double类型,占据64位。
String:可变字符类型,映射到数据库中是varchar类型.
Boolean:布尔类型,映射到数据库中的是tinyint类型。
DECIMAL:定点类型。是专门为了解决浮点类型精度丢失的问题的。在存储钱相关的字段的时候建议大家都使用这个数据类型。并且这个类型使用的时候需要传递两个参数,第一个参数是用来标记这个字段总能能存储多少个数字,第二个参数表示小数点后有多少位。
Enum:枚举类型。指定某个字段只能是枚举中指定的几个值,不能为其他值。在ORM模型中,使用Enum来作为枚举
Date:存储时间,只能存储年月日。映射到数据库中是date类型。在Python代码中,可以使用`datetime.date`来指定
DateTime:存储时间,可以存储年月日时分秒毫秒等。映射到数据库中也是datetime类型。在Python代码中,可以使用`datetime.datetime`来指定。
Time:存储时间,可以存储时分秒。映射到数据库中也是time类型。在Python代码中,可以使用`datetime.time`来至此那个。
Text:存储长字符串。一般可以存储6W多个字符。如果超出了这个范围,可以使用LONGTEXT类型。映射到数据库中就是text类型。
LONGTEXT:长文本类型,映射到数据库中是longtext类型。
SQLAlchemy列选项
primary_key 如果设为True,这列就是表的主键
unique 如果设为True,这列不允许出现重复的值
index 如果设为True,这列创建索引,提升查询效率
nullable 如果设为True,这列允许使用空值;如果设为False,这列不允许使用空值
default 为这列定义默认值
python manager.py db --help
python manager.py db init
python manager.py db migrate
python manager.py db upgrade
python manager.py db downgrade
"""
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate,MigrateCommand
from flask_script import Shell,Manager
import os
env = os.environ
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = env['SQLALCHEMY_DATABASE_URI']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
migrate = Migrate(app, db) #第一个参数是Flask的实例,第二个参数是Sqlalchemy数据库实例
manager = Manager(app)
manager.add_command('db',MigrateCommand) #manager是Flask-Script的实例,这条语句在flask-Script中添加一个db命令
class Image(db.Model):
__tablename__ = 'images'
__table_args__ = {'mysql_charset':'utf8', 'mysql_comment': '图片资源表'}
id = db.Column(db.Integer, autoincrement=True, nullable=False, primary_key=True, comment='主键')
file_path = db.Column(db.String(500), nullable=True, comment='文件路径')
source_type = db.Column(db.String(20), nullable=True, comment='数据源类型')
source_pid = db.Column(db.String(30), nullable=True, comment='数据源PID')
source_brand = db.Column(db.String(50), nullable=True, comment='数据源品牌')
source_category = db.Column(db.String(50), nullable=True, comment='数据源分类')
source_color = db.Column(db.String(20), nullable=True, comment='数据源颜色')
built_ann_index = db.Column(db.Boolean, nullable=True, comment='是否已提取特征值')
duplicated = db.Column(db.Boolean, nullable=True, comment='特征值是否重复')
created_at = db.Column(db.DateTime, nullable=True, comment='创建时间')
class Model(db.Model):
__tablename__ = 'models'
__table_args__ = {'mysql_charset':'utf8', 'mysql_comment': '模型表'}
id = db.Column(db.Integer, autoincrement=True, nullable=False, primary_key=True, comment='主键')
type = db.Column(db.String(20), nullable=True, comment='模型类型')
name = db.Column(db.String(30), nullable=True, comment='模型名称')
created_at = db.Column(db.DateTime, nullable=True, comment='创建时间')
updated_at = db.Column(db.DateTime, nullable=True, comment='更新时间')
class Label(db.Model):
__tablename__ = 'labels'
__table_args__ = {'mysql_charset':'utf8', 'mysql_comment': '标签表'}
id = db.Column(db.Integer, autoincrement=True, nullable=False, primary_key=True, comment='主键')
model_id = db.Column(db.Integer, autoincrement=False, nullable=True, comment='模型ID')
value = db.Column(db.String(50), nullable=True, comment='标签')
annotated_amount = db.Column(db.Integer, autoincrement=False, nullable=True, comment='标记数量')
created_at = db.Column(db.DateTime, nullable=True, comment='创建时间')
updated_at = db.Column(db.DateTime, nullable=True, comment='更新时间')
class Annotation(db.Model):
__tablename__ = 'annotations'
__table_args__ = {'mysql_charset':'utf8', 'mysql_comment': '标记表'}
id = db.Column(db.Integer, autoincrement=True, nullable=False, primary_key=True, comment='主键')
image_id = db.Column(db.Integer, autoincrement=False, nullable=True, comment='资源ID')
label_id = db.Column(db.Integer, autoincrement=False, nullable=True, comment='标签ID')
model_id = db.Column(db.Integer, autoincrement=False, nullable=True, comment='模型ID')
created_at = db.Column(db.DateTime, nullable=True, comment='创建时间')
updated_at = db.Column(db.DateTime, nullable=True, comment='更新时间')
class Training(db.Model):
__tablename__ = 'trainings'
__table_args__ = {'mysql_charset':'utf8', 'mysql_comment': '训练表'}
id = db.Column(db.Integer, autoincrement=True, nullable=False, primary_key=True, comment='主键')
model_id = db.Column(db.Integer, autoincrement=False, nullable=True, comment='模型ID')
serve_version = db.Column(db.String(30), nullable=True, comment='模型部署的标示')
info = db.Column(db.Text, nullable=True, comment='信息info')
status = db.Column(db.Boolean, nullable=True, comment='训练状态')
log_filename = db.Column(db.String(500), nullable=True, comment='训练日志文件目录')
created_at = db.Column(db.DateTime, nullable=True, comment='创建时间')
updated_at = db.Column(db.DateTime, nullable=True, comment='更新时间')
class User(db.Model):
__tablename__ = 'users'
__table_args__ = {'mysql_charset':'utf8', 'mysql_comment': '用户表'}
id = db.Column(db.Integer, autoincrement=True, nullable=False, primary_key=True, comment='主键')
username = db.Column(db.String(30), nullable=False, comment='用户名')
email = db.Column(db.String(30), nullable=False, comment='email')
encrypted_password = db.Column(db.String(100), nullable=False, comment='密码')
auth_token = db.Column(db.String(100), nullable=True, comment='Token')
created_at = db.Column(db.DateTime, nullable=True, comment='创建时间')
updated_at = db.Column(db.DateTime, nullable=True, comment='更新时间')
if __name__ == '__main__':
manager.run()
error
An error occurred while installing mysqlclient! Will try again.
Installing initially failed dependencies…
[InstallError]: File "/home/kefu/.local/lib/python3.6/site-packages/pipenv/cli/command.py", line 252, in install
[InstallError]: site_packages=state.site_packages
[InstallError]: File "/home/kefu/.local/lib/python3.6/site-packages/pipenv/core.py", line 2062, in do_install
[InstallError]: keep_outdated=keep_outdated
[InstallError]: File "/home/kefu/.local/lib/python3.6/site-packages/pipenv/core.py", line 1314, in do_init
[InstallError]: pypi_mirror=pypi_mirror,
[InstallError]: File "/home/kefu/.local/lib/python3.6/site-packages/pipenv/core.py", line 901, in do_install_dependencies
[InstallError]: retry_list, procs, failed_deps_queue, requirements_dir, **install_kwargs
[InstallError]: File "/home/kefu/.local/lib/python3.6/site-packages/pipenv/core.py", line 796, in batch_install
[InstallError]: _cleanup_procs(procs, failed_deps_queue, retry=retry)
[InstallError]: File "/home/kefu/.local/lib/python3.6/site-packages/pipenv/core.py", line 703, in _cleanup_procs
[InstallError]: raise exceptions.InstallError(c.dep.name, extra=err_lines)
[pipenv.exceptions.InstallError]: Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
[pipenv.exceptions.InstallError]: Collecting mysqlclient
[pipenv.exceptions.InstallError]: Using cached https://pypi.tuna.tsinghua.edu.cn/packages/a5/e1/e5f2b231c05dc51d9d87fa5066f90d1405345c54b14b0b11a1c859020f21/mysqlclient-2.0.1.tar.gz (87 kB)
[pipenv.exceptions.InstallError]: ERROR: Command errored out with exit status 1:
[pipenv.exceptions.InstallError]: command: /home/kefu/.local/share/virtualenvs/fashion-dna-meNMHv4N/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-f50hz8qf/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-f50hz8qf/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-4ksd5ia5
[pipenv.exceptions.InstallError]: cwd: /tmp/pip-install-f50hz8qf/mysqlclient/
[pipenv.exceptions.InstallError]: Complete output (12 lines):
[pipenv.exceptions.InstallError]: /bin/sh: 1: mysql_config: not found
[pipenv.exceptions.InstallError]: /bin/sh: 1: mariadb_config: not found
[pipenv.exceptions.InstallError]: /bin/sh: 1: mysql_config: not found
[pipenv.exceptions.InstallError]: Traceback (most recent call last):
[pipenv.exceptions.InstallError]: File "<string>", line 1, in <module>
[pipenv.exceptions.InstallError]: File "/tmp/pip-install-f50hz8qf/mysqlclient/setup.py", line 15, in <module>
[pipenv.exceptions.InstallError]: metadata, options = get_config()
[pipenv.exceptions.InstallError]: File "/tmp/pip-install-f50hz8qf/mysqlclient/setup_posix.py", line 65, in get_config
[pipenv.exceptions.InstallError]: libs = mysql_config("libs")
[pipenv.exceptions.InstallError]: File "/tmp/pip-install-f50hz8qf/mysqlclient/setup_posix.py", line 31, in mysql_config
[pipenv.exceptions.InstallError]: raise OSError("{} not found".format(_mysql_config_path))
[pipenv.exceptions.InstallError]: OSError: mysql_config not found
[pipenv.exceptions.InstallError]: ----------------------------------------
[pipenv.exceptions.InstallError]: ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
ERROR: Couldn't install package: mysqlclient
Package installation failed...
☤ ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/1 — 0
本机必须装mysqlclient
pip install mysqlclient
sudo apt install libmysqlclient-dev