1

My structure:

rootdir
- app
  - alembic
    - env.py
  - __init__.py
  - alembic.ini
  - models.py

My env.py has:

from app.models import *
from app import db
target_metadata = db.metadata

I get the error:

ImportError: No module named 'app'

when I run:

app# alembic revision --autogenerate -m 'init'

Tjorriemorrie
  • 16,818
  • 20
  • 89
  • 131

1 Answers1

3

You can get the app from Flask via the current_app property; and then use it as follows:

from flask import current_app
config.set_main_option('sqlalchemy.url', current_app.config.get('SQLALCHEMY_DATABASE_URI'))
target_metadata = current_app.extensions['migrate'].db.metadata

I would recommend using Flask-Migrate as it sets this up automatically.

PS: Thanks for the assholes that downvote and not helping.

Tjorriemorrie
  • 16,818
  • 20
  • 89
  • 131
  • It's too bad you got the downvotes. People should leave a comment in addition to downvoting to explain why. Anyway, the question is valid and provided information so I upped one. Even if people made though, I'd say try not to swear :D It was funny to see tho – chrips Mar 18 '19 at 09:59