定义:ask 扩展封装了 SQLAlchemy 框架。在 Flask-SQLAlchemy 中,数据库使用 URL 指定,下表列出了常见的数据库引擎和对应的 URL。
安装:pip install flask-sqlalchemy
pip install flask-migrate
# 启动文件
from flask_script import Manager,Server from flask_migrate import MigrateCommand,Migrate from rbac import create_app,db app = create_app() manager = Manager(app) migrate = Migrate(app,db) manager.add_command("runserver",Server()) manager.add_command("db",MigrateCommand) if __name__ == '__main__': manager.run()
# 启动文件启动自动触发
from flask import Flask from config.setting import BaseConfig #导入flask-sqlalchemy from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() from .views.user import user # 导入models中的表 from .model.models import * def create_app(): app = Flask(__name__) app.register_blueprint(user) db.init_app(app) return app
执行命令:
初次:
python manage.py db init
python manage.py db migrate
python manage.py db upgrade
二:(查看)生成当前环境的所有插件
生成当前环境的所有依赖: requirements.txt
pip3 freeze > requirements.txt
生成当前程序的所有依赖: requirements.txt
pip3 install pipreqs
pipreqs ./