赞
踩
ModuleNotFoundError: No module named 'flask._compat'
Flask 2.0以上的版本已经移除_compat这个模块,所以与Flask-Script不在兼容。flask_script库已经4年多没有进行更新了,而且该作者也在issue上表示过不再对这个库后续进行维护,取而代之的是Flask内置的cli库已经替代了这个manager的命令行管理机制
**
**
以下是requirements.txt参考
alembic1.4.2
aniso86019.0.1
APScheduler3.7.0
attrs22.1.0
certifi2022.9.24
chardet3.0.4
click8.1.3
colorama0.4.5
elasticsearch7.17.6
et-xmlfile1.1.0
Flask==1.1.2
Flask-Cors3.0.3
**Flask-Migrate2.5.3**
Flask-RESTful0.3.7
**Flask-Script2.0.6**
Flask-SQLAlchemy==2.4.4
fluent-logger0.9.6
idna2.8
importlib-metadata5.0.0
**itsdangerous1.1.0**
jdcal1.4.1
Jinja22.11.3
jsonpointer2.2
jsonschema3.0.1
Mako1.2.3
**MarkupSafe2.0.1**
msgpack0.6.2
openpyxl2.4.8
PyMySQL0.9.3
pyrsistent0.18.1
python-dateutil2.8.2
python-editor1.0.4
pytz2022.4
PyYAML5.1.2
requests2.22.0
six1.16.0
SQLAlchemy1.3.23
SQLAlchemy-Utils0.36.5
typing_extensions4.4.0
tzlocal2.1
urllib31.25.11
Werkzeug1.0.1
xlrd1.2.0
xlwt1.3.0
zipp==3.9.0
首先要在虚拟环境中安装Flask-Migrate(记得指定版本)。
pip install flask-migrate==2.5.3
flask-migrate是基于alembic进行的一个封装,并集成到flask中,而所有的迁移操作其实都是alembic做的,它能跟踪模块的变化,并将变化映射到数据库中。
First
manage.py
from flask import Flask from flask_migrate import Migrate,MigrateCommand from flask_script import Shell,Manager from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) manager = Manager(app) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:password@127.0.0.1:3306/db_name?charset=utf8mb4' app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True db = SQLAlchemy(app) #第一个参数是Flask的实例,第二个参数是Sqlalchemy数据库实例 migrate = Migrate(app,db) #manager是Flask-Script的实例,这条语句在flask-Script中添加一个db命令 manager.add_command('db',MigrateCommand) class Role(db.Model): # 定义表名 __tablename__ = 'roles' # 定义列对象 id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) email = db.Column(db.String(64), unique=True) age = db.Column(db.String(64), unique=True) def __repr__(self): return 'Role:'.format(self.name) #定义用户 class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), unique=True, index=True) def __repr__(self): return 'User:'.format(self.username) if __name__ == '__main__': manager.run()
创建迁移仓库
python manage.py db init
创建迁移脚本(双引号)
python manage.py db migrate -m "initial migration"
更新数据库
python manage.py db upgrade
回退数据库
python manage.py db downgrade 版本号
Second
manage.py
from flask import Flask from flask_migrate import Migrate,MigrateCommand from flask_script import Shell,Manager import models.users #这个一定要导入(表) from exts import db app = Flask(__name__) manager = Manager(app) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:tsq123@127.0.0.1:3306/leqi?charset=utf8mb4' app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True #第一个参数是Flask的实例,第二个参数是Sqlalchemy数据库实例 migrate = Migrate(app,db) #manager是Flask-Script的实例,这条语句在flask-Script中添加一个db命令 manager.add_command('db',MigrateCommand) if __name__ == '__main__': manager.run()
users.py
from exts import db
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer,primary_key=True,autoincrement=True)
username = db.Column(db.String(50),nullable=False)
exts.py(防止循环引用报错)
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
目录结构
创建迁移仓库
创建迁移脚本
更新数据库
回退数据库
Third
manage.py
from flask_script import Manager
from run import app
from exts import db
import models.users #这个一定要导入
from flask_migrate import Migrate,MigrateCommand
manager = Manager(app)
#第一个参数是Flask的实例,第二个参数是Sqlalchemy数据库实例
migrate = Migrate(app,db)
#manager是Flask-Script的实例,这条语句在flask-Script中添加一个db命令
manager.add_command("db",MigrateCommand) #把所有命令放到db里面
if __name__ == '__main__':
manager.run()
users.py
from exts import db
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer,primary_key=True,autoincrement=True)
username = db.Column(db.String(50),nullable=False)
config.py
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:password@127.0.0.1:3306/db_name?charset=utf8mb4'
exts.py(防止循环引用报错)
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
run.py
from flask import Flask from exts import db import config app = Flask(__name__) app.config.from_object(config) # app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:tsq123@127.0.0.1:3306/leqi?charset=utf8mb4' db.init_app(app) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
目录结构
**
**
所需库的版本
Flask>=2.0.0
Flask-Migrate>=3.0.0
app.py
from flask import Flask
from flask_migrate import Migrate
app = Flask(__name__)
# 让迁移时,app和db建立关联
Migrate(app,db)
if __name__ == '__main__':
app.run()
最新的命令行如下:
flask db init
flask db migrate
flask db upgrade
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。