Migrations with Alembic¶
Typically, you’ll want to use Alembic to generate and run migrations as you create and modify your tables. The Flask-Alembic extension provides integration between Flask, Flask-SQLAlchemy(-Lite), and Alembic. You can also use Alembic directly, but it will require a little more setup.
Flask-Alembic¶
Flask-Alembic will use the engines configured by Flask-SQLAlchemy-Lite. You need to tell it about the metadata object for your base model. Multiple databases are also supported, see the Flask-Alembic docs for details.
from flask import Flask
from flask_alembic import Alembic
from flask_sqlalchemy_lite import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class Model(DeclarativeBase):
pass
class User(Model):
__tablename__ = "user"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str]
db = SQLAlchemy()
alembic = Alembic(metadatas=Model.metadata)
def create_app():
app = Flask(__name__)
app.config["SQLALCHEMY_ENGINES"] = {"default": "sqlite:///default.sqlite"}
app.config.from_prefixed_env()
db.init_app(app)
alembic.init_app(app)
return app
$ flask db revision 'init'
$ flask db upgrade
Plain Alembic¶
You’ll need to modify the migrations/env.py
script that Alembic generates to
tell it about your Flask application and the engine and metadata.
$ alembic init migrations
Modify parts of migrations/env.py
, the ...
are omitted parts of the file.
from project import create_app, Model, db
flask_app = create_app()
...
target_metadata = Model.metadata
...
def run_migrations_online() -> None:
with flask_app.app_context():
connectable = db.engine
...
...
$ alembic revision --autogenerate -m 'init'
$ alembic upgrade head