2
votes

I am coming from Django background. In flask I am looking for the migration command. How should I use the migration-script for this purpose. I referred the document(https://flask-migrate.readthedocs.io/en/latest/#using-flask-script) for the same.

My project structure is like.

  1. service
    • init.py
    • models
      • init.py
      • employee.py (contains model declaration)
    • manage.py
    • app.py (start point of project)
    • urls.py (Used blueprint for url routing)

As per the document if I written my models in manage.py file then it will work as expected. But I have models folder where I am writing all the models. So if I hit python3 manage.py db migrate command it will not detect any changes from the models file.

  • Imported model in init.py file of the models.
  • imported models in manage.py file.

Manage.py file.

from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager

from app import app, db

migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)

if __name__ == '__main__':
    manager.run()

Currently it will only create alembic_version table in my database

This is the output

>> python3 manage.py db migrate
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.env] No changes in schema detected.
2

2 Answers

2
votes

I found the solution. Default flask-migrate looks model in the same file where we define our migration(In my case it is manage.py file).

Here I created models inside my models package (package name can be any)

To solve this issue we need to perform two steps. 1) Import model in init.py file of model package. 2) Import model just after we define

db = SQLAlchemy(app)

command. That import statement is unused but it will tell flask-migrate that models are preset in models package.

1
votes

Try running flask db migrate command. If it does not help, try importing your models in manage.py script.