flask, alembic and blueprints

For some time I could easily do without autogenerated migrations. Now I wanted them and I wanted to use Flask and not Django. I started, very naively, by installing and importing either flask-alembic and flask-migrate but they all seemed (at that time) to support patterns that I didn’t want (e.g. manager, single models.py) or couldn’t understand. At some points I didnt’t get migrations to work at all or they were empty or blueprints wouldn’t work or…

What I wanted was
* a folder “models” containing all models with a file for each model
* plain alembic
* a single start file with my setup and configs

After installing alembic via pip migrations didn’t work and even importing model in env.py didn’t solve it, fiddeling with target_metadata didn’t help as well as several other solutions outlined in StackOverflow. So here is what worked for me:

In my start/setup file (start.py in my case) has a function:


start_app():
app = Flask(__name__)
# config stuff
db.init_app(app)
return app

and


if __name__ == "__main__":
app.start_app()
app.run()

The app is started by just running python start.py without need of a manager.

I created a my shared_model that all model import:


from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

This makes it easier since all the models just import this shared model and I can also put some other stuff in here that I want to have access to in my models.

The last thing to do is editing the alembic env.py:
1. Import the start_app function and start the app
2. Import the db from the shared model and initialize it
3. Configure and set target_metadata


from start import start_app
app = start_app()
from models.shared_model import db
db.init_app(app)
config.set_main_option("sqlalchemy.url", app.config["SQLALCHEMY_DATABASE_URI"])
target_metadata = db.metadata

That’s about it models go into the models folder and can be used in blueprints, alembic revision –autogenerate produces more than “pass” and the app starts like usual.

Advertisements