Flask-Migrate: Alembic does not detect models.

I set up Flask-Migrate using your suggested changes to __init__.py and initialized the migrations and attempted to generate a file. However, this migration showed no changes to the db even though I had clearly added a users table.

.
├── Procfile
├── README.md
├── TODO.md
├── bin
├── calpoly
├── config.pyc
├── flask_migrate
├── include
├── lib
├── manage.py
├── migrations
├── requirements.txt
├── run.py
└── runtime.txt

Here’s the migration:

"""empty message

Revision ID: 2812f95cc1be
Revises: None
Create Date: 2013-09-17 23:05:53.508999

"""

# revision identifiers, used by Alembic.
revision = '2812f95cc1be'
down_revision = None

from alembic import op
import sqlalchemy as sa


def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    pass
    ### end Alembic commands ###


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    pass
    ### end Alembic commands ###

And finally, the code.

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 17 (6 by maintainers)

Most upvoted comments

Unfortunately I could not easily run your code to test it, it seems there are several things that need to be provided in the environment.

Models are typically imported in the app package, so basically in the __init__.py file.

You know what, I think I know what’s going on. Did you try deleting your database? If the database matches your model definition then Alembic will not detect any changes.

Just as a test, define a quick model in __init__.py, something like:

class Test(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(64))

and see if that is recognized by the migration.

One more tip, I met the same problem, but my solution is to check the module load order.

if your model definition python script,( such as XXX.py) is not in the app init load process, it will not find the model you create.

The case like, you import db to app.py and use db.init(app), but the model definition is ingnored. so you better import the model-define python script into your app.py.