初始化代码 extensions.py
from flask_bcrypt import Bcrypt
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from authentek.logger import log
db = SQLAlchemy()
migrate = Migrate()
bcrypt = Bcrypt()
cors = CORS(resources={r"/v1/*": {"origins": "*"}})
log.debug("in debug mode")
from authentek.extensions import db, migrate, bcrypt, cors
def configure_extensions(flask_app, cli):
"""configure flask extensions
"""
db.init_app(flask_app)
blueprint = Blueprint('api', __name__, url_prefix='/v1')
api.init_app(blueprint)
api.add_namespace(users_namespace)
api.add_namespace(auth_namespace)
if blueprint.name not in flask_app.blueprints.keys():
flask_app.register_blueprint(blueprint)
else:
flask_app.blueprints[blueprint.name] = blueprint
cors.init_app(flask_app)
db.app = flask_app
db.create_all()
bcrypt.init_app(flask_app)
if cli is True:
migrate.init_app(flask_app, db) # here it's registered
flask db migrate
2020-06-27 09:09:04,827 - authentek.logger - DEBUG - in debug mode
Traceback (most recent call last):
File "/usr/local/bin/flask", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 967, in main
cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None)
File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 586, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.7/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python3.7/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python3.7/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.7/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/click/decorators.py", line 21, in new_func
return f(get_current_context(), *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 426, in decorator
return __ctx.invoke(f, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/flask_migrate/cli.py", line 88, in migrate
rev_id)
File "/usr/local/lib/python3.7/site-packages/flask_migrate/__init__.py", line 175, in migrate
config = current_app.extensions['migrate'].migrate.get_config(
KeyError: 'migrate'
答案 0 :(得分:0)
您尚未初始化migrate
实例。
def configure_extensions(flask_app, cli):
"""configure flask extensions
"""
db.init_app(flask_app)
migrate.init_app(flask_app, db)
# ...
此外,由于您希望Flask-Migrate管理数据库架构,因此删除db.create_all()
。