Skip to main content
This article explains how to set up Alembic for migration management with SQLAlchemy, specifically within a FastAPI project. You will learn how to install, initialize, and configure Alembic to keep track of your database schema changes.

Project Structure

Below is an example of the project structure, which shows where migration scripts will be stored:

Installing Alembic

Begin by installing Alembic using pip. This not only installs the package but also provides access to its command-line interface:
After installing, verify the installation by running:
You should see output similar to this:
Before moving forward, note that the sample code below presents snippets of your SQLAlchemy models. For instance, you could have models for User and Vote as follows:
In this example, you are instructed to remove the phone_number field once Alembic is set up and you are ready to update your database schema.

Preparing the Database

Before running migrations, ensure that your application is stopped to prevent automatic restarts during the migration process. If required, drop any existing tables—using DROP CASCADE if there are foreign key constraints—to start with a clean slate. To check the current data in your database, you can run the following SQL query:

Initializing Alembic

Initialize Alembic to create the necessary directory structure and configuration files. The Alembic CLI provides several subcommands, which you can review using:
You can also check help for initialization by running:
When ready, initialize Alembic by specifying a directory name. For example:
The output should indicate that the directory structure and configuration files have been created successfully:
This creates the Alembic directory outside the application folder along with the alembic.ini configuration file. Next, you need to update both the env.py and alembic.ini files to connect Alembic with your SQLAlchemy models and database settings.

Configuring env.py for SQLAlchemy

The env.py file is the main configuration file for Alembic. It must be updated to import your SQLAlchemy models and set the target metadata for autogeneration. An initial snippet might look like this:
To work with SQLAlchemy models, update the file to import your base object from your database module. For example:
Then, modify your env.py file to import Base from your application’s database file. Replace the original metadata configuration with:
If your models are defined or imported from a different file (e.g., app.models), ensure the import reflects that. For instance, to detect changes in a Post model:
Then, update your env.py accordingly:
This configuration ensures that Alembic can access your SQLAlchemy models and automatically track schema changes.

Configuring alembic.ini

Within the alembic.ini file, specify your SQLAlchemy database URL. Initially, it might appear as follows:
Replace the placeholder connection string with your actual database credentials. For example, if you are using PostgreSQL:
While the above example hardcodes credentials for demonstration purposes, it is advisable to manage sensitive information using environment variables in production.

Overriding the Database URL in env.py

To avoid hardcoding credentials within alembic.ini, you can override the SQLAlchemy URL directly in env.py using values from your configuration file. For example:
This method allows you to securely manage database credentials using environment variables. A typical Pydantic settings class defined in config.py might resemble:
You can then use these settings in your main application as follows:

Final Remarks

After completing these configurations, Alembic will be connected to your PostgreSQL database and ready to detect changes in your SQLAlchemy models. This setup streamlines the process of generating migration scripts and applying database updates. To create and run migrations, use the following commands:
Happy migrating!

Watch Video