Database Migration Commands
The Database migration commands provide an easy way to manage your database schema changes using Alembic.
Basic Usage
This will create and apply migrations to your database based on your SQLModel models.
Available Commands
Command | Description |
---|---|
makemigrations |
Create new database migrations |
migrate |
Apply pending database migrations |
shell |
Open a shell in the API container |
env_check |
Check for missing environment variables |
Command Details
Makemigrations
Creates new database migration files based on detected changes in your SQLModel models.
Options:
Option | Short | Description |
---|---|---|
--message |
-m |
Message describing the migration (default: "init") |
This command will:
- Detect changes in your SQLModel models
- Generate migration files in the
migrations/versions
directory - Each migration file contains upgrade and downgrade functions
Migrate
Applies all pending database migrations to bring your database schema up to date.
This command will:
- Run all pending migrations in sequence
- Apply schema changes to your database
- Update the Alembic version table to track applied migrations
Shell
Opens a shell in the API Docker container for direct access to the environment.
This shell allows you to:
- Run Python code with access to your models and environment
- Execute direct Alembic commands for advanced operations
- Access the database directly
Example shell usage:
# Once in the shell
alembic current # Show current migration version
alembic history # Show migration history
alembic downgrade -1 # Downgrade one version
How It Works
The migration commands work through the following process:
- Model Detection:
- Migrations scan the models defined in
app/core/main_models.py
-
All imported SQLModel classes are detected for changes
-
Migration Storage:
- Migration files are stored in
migrations/versions/
-
Each migration is tracked with a unique revision ID
-
Docker Integration:
- Commands run through Docker Compose to ensure consistent environments
- Uses the API container defined in your docker-compose.yml
Common Issues
- Model Not Found: Ensure models are imported in
app/core/main_models.py
- Migration Conflicts: If multiple developers create migrations, merge them carefully
- Failed Migrations: Use the shell to downgrade to a previous version
- Empty Migrations: No changes detected in models since last migration
- Database Connection Issues: Check database URL in settings or environment variables
Troubleshooting
If you encounter issues with migration commands:
- Migration Creation Failed:
- Ensure all models are properly imported in main_models.py
- Check for syntax errors in your model definitions
-
Verify database connection settings
-
Migration Application Failed:
- Check the migration files for complex operations that might fail
- Look at database logs for specific SQL errors
-
Try running specific migrations directly in the shell
-
No Changes Detected:
- Confirm your model changes are in the right files
-
Make sure changes are significant (adding fields, changing types, etc.)
-
Advanced Fixes:
- Use the shell to run specific Alembic commands
- Check Alembic's current version with
alembic current
- Execute SQL directly if needed for complex situations
- Use
fastapi-admin shell
to access the container's environment
Best Practices
- Meaningful Messages: Use descriptive -m messages for each migration
- Regular Migrations: Create small, focused migrations rather than large changes
- Test Migrations: Always test migrations in development before production
- Version Control: Commit migration files to your version control system
- Database Backups: Always backup your database before applying migrations in production
For more information on Alembic and SQLModel, refer to their official documentation.