Issue
I'm trying to set up Flask Migrate to handle my migrations for my app. I use a factory to handle my set-up and when I try to run flask db init I get the following error:
Traceback (most recent call last):
File "/Users/grahammorby/Documents/GitHub/tagr-api/venv/bin/flask", line 8, in <module>
sys.exit(main())
^^^^^^
File "/Users/grahammorby/Documents/GitHub/tagr-api/venv/lib/python3.12/site-packages/flask/cli.py", line 1064, in main
cli.main()
File "/Users/grahammorby/Documents/GitHub/tagr-api/venv/lib/python3.12/site-packages/click/core.py", line 1078, in main
rv = self.invoke(ctx)
^^^^^^^^^^^^^^^^
File "/Users/grahammorby/Documents/GitHub/tagr-api/venv/lib/python3.12/site-packages/click/core.py", line 1682, in invoke
cmd_name, cmd, args = self.resolve_command(ctx, args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/grahammorby/Documents/GitHub/tagr-api/venv/lib/python3.12/site-packages/click/core.py", line 1729, in resolve_command
cmd = self.get_command(ctx, cmd_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/grahammorby/Documents/GitHub/tagr-api/venv/lib/python3.12/site-packages/flask/cli.py", line 579, in get_command
app = info.load_app()
^^^^^^^^^^^^^^^
File "/Users/grahammorby/Documents/GitHub/tagr-api/venv/lib/python3.12/site-packages/flask/cli.py", line 313, in load_app
app = locate_app(import_name, None, raise_if_not_found=False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/grahammorby/Documents/GitHub/tagr-api/venv/lib/python3.12/site-packages/flask/cli.py", line 219, in locate_app
__import__(module_name)
File "/Users/grahammorby/Documents/GitHub/tagr-api/wsgi.py", line 3, in <module>
app = init_app()
^^^^^^^^^^
File "/Users/grahammorby/Documents/GitHub/tagr-api/application/__init__.py", line 26, in init_app
Migrate = Migrate(app, db)
UnboundLocalError: cannot access local variable 'Migrate' where it is not associated with a value
and My set app application looks as follows:
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from flask_bcrypt import Bcrypt
from flask_jwt_extended import JWTManager
from flask_migrate import Migrate
# Global accessible libraries
db = SQLAlchemy()
ma = Marshmallow()
bcrypt = Bcrypt()
JWTManager = JWTManager()
def init_app():
app = Flask(__name__, instance_relative_config=False)
app.config.from_object(Config)
# Setup plugins
db.init_app(app)
ma.init_app(app)
bcrypt.init_app(app)
JWTManager.init_app(app)
Migrate = Migrate(app, db)
from models import User, App
with app.app_context():
# Blueprints
from application.users import users_blueprint
from application.apps import apps_blueprint
app.register_blueprint(users_blueprint)
app.register_blueprint(apps_blueprint)
return app
I have tried to use it with init_app but that caused errors as well
Solution
You are getting an UnboundLocalError exception as you are using the same name for a function local variable and a module level variable in the line Migrate = Migrate(app, db)
in your init_app function. Due to python variable scoping rule, the fact that you make an assignment to Migrate
within the function means that a new local variable is created with that name and you lose access to the module level Migrate
that you imported from flask_migrate. This can be solved by using a different name for the variable within the function.
Answered By - EAW
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.