Flask-Migrate: flask db init fails: KeyError 'migrate'
Hi Miguel!
I’m attempting to use your flask-migrate CLI tool as per your tutorial.
Issue:
Running flask db init from the terminal fails with error:
directory = current_app.extensions['migrate'].directory
KeyError: 'migrate'
Desired result:
flask db init should create the database (sqlite) and initialize database tables from all models.
Context:
- I’ve exported
FLASK_APP='run.py', whererun.pydoes the import of ‘app’ and ‘db’ from(myapp)/__init__.py - Flask app is created with:
app = Flask(APP_NAME)in run.py - DB object is created with:
db = SQLAlchemy(app)in run.py
Speculative analysis:
It seems that current_app.extensions is not somehow being populated with a desired key migrate, which I assume refers to your Flask extension.
Where/how is that supposed to happen?
There may be steps here I’ve missed or required parameters I’m unaware of. Can you point me in the right direction?
Thanks!
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 7
- Comments: 25 (7 by maintainers)
Links to this issue
Commits related to this issue
- #39 Make it such that flask db init can run https://github.com/miguelgrinberg/Flask-Migrate/issues/196#issuecomment-381343393 — committed to themotte/rDrama by faul-sname 2 years ago
- Add migrations using alembic. * #39 Add Flask-Migrate dep * #39 Make it such that flask db init can run https://github.com/miguelgrinberg/Flask-Migrate/issues/196#issuecomment-381343393 * Ru... — committed to themotte/rDrama by faul-sname 2 years ago
Hi fieldse. Did you create a Migrate object? I get the same error when trying
flask db initwithout that object.@utsushiiro You’re a wizard. 😄 @miguelgrinberg Correct, that was it.
Yeah, I had abstracted the
migrate = Migrate(app, db)to a separate function.Solution:
Adding
migrate = Migrate(app, db)into__init__.pyresolved the issue. (I didn’t realize this was necessary for the cli functionality and database initialization, assumed it was related only to database migrations afterwards.)Running
flask db initnow works correctly:Thanks guys!
Yes, I think @utsushiiro is right, you did not initialize the Flask-Migrate extension by creating a
Migrateobject.@phstrauss this line in app/init.py:
should be:
I have a
manage.pyin the project base directory, and init the migrations by run@Nerevarishe does it still do it if you remove the
app.run()call inapp/__init__.py. If you are going to use the Flask CLI you have to start the app withflask run, don’t useapp.run().@malay95 if you project on Win Machine try to go with PowerShell to
\venvfoulder of Project, (install again all packadges (now we installing not for venv enviroment only) WTF and etc.). then type:$env:FLASK_APP = "<main_file_where_app.run()>.py"then use flask commands:flask db init@miguelgrinberg I Have changed the init code to this: import os from flask import Flask, request, current_app from config import Config from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate
db = SQLAlchemy() migrate = Migrate()
def create_app(test_config=None): app = Flask(name, instance_relative_config=True) app.config.from_object(Config)
And I no longer have the error! Many thanks for your help and most of all for your excellent tutorials and hard work with python.