I have my Flask project hierarchy a
project
├── controllers
└── models
└── schema.py
When I run python schema.py db init
, a migrations
folder is added under project
instead of under models
. I have a __init__.py
under all 3 folders (not showing here for brevity). I want the migrations
folder generated under models
. How do I do it?
With the above application you can create a migration repository with the following command: This will add a migrations folder to your application. The contents of this folder need to be added to version control along with your other source files. $ flask db migrate -m "Initial migration."
The classes which are exposed are Migrate and MigrateCommand. Migrate class itself covers all the functionality and utility of the extension whereas MigrateCommand is used when there is a need of exposing the database migration commands through the usage of the Flask script extension.
Installation of the Flask migrate module in Python environment: Initializing the migrate instance: Adding the MigrateCommand into the list of commands in the manager: In conclusion, in this article, we have learned about the migrate configuration and application in Flask.
Flask-Migrate is an extension that configures Alembic in the proper way to work with your Flask and Flask-SQLAlchemy application. In terms of the actual database migrations, everything is handled by Alembic so you get exactly the same functionality. This is an example application that handles database migrations through Flask-Migrate:
Well.. Like Oluwafemi said, you can pass the -d (--directory) flag to your manager script in the cli command
python schema.py db init --directory models/migrations
The problem with this solution is that you will have to specify the migration path each time you will enter a db command, otherwise you will get the following error:
alembic.util.exc.CommandError: Path doesn't exist: 'migrations'. Please use the 'init' command to create a new scripts folder.
A better way to config your migration path is by passing the directory
argument to the Migrate
object in your manage.py (or in your case, the 'schema.py').
For example:
import os
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from .application import app, db
MIGRATION_DIR = os.path.join('models', 'migrations')
migrate = Migrate(app, db, directory=MIGRATION_DIR)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
Besides, I suggest you move all the command interface functionality from 'schema.py' to a diffrent python script in your application root path (like, 'manager.py') and create the migrations folder next to your models and not inside it. The models folder should not contains anything else than the Data Model Objects!
Hope it was helpful..
You need to pass the directory option to the init command. This can be the path to the migrations directory. It is set to migrations by default.
python schema.py db init --directory models/migrations
Or you can update the location of scripts location in alembic.ini
# path to migration scripts
script_location = alembic
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With