Flask-migrate: flask db migrate not detecting new table

Created on 12 Apr 2019  路  3Comments  路  Source: miguelgrinberg/Flask-Migrate

Hello @miguelgrinberg

Thanks for putting together this package, it's overall very convenient to use. I'm now having an issue, however.

It seems that alembic is not detecting new tables (models) that are added via my models.py file. At this point when I run flask db migrate the only output I get from the script is:

INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.

And nothing else. I then, based on suggestions in other issues such as #8 I added a test table:

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

however I still get the same results with flask db migrate. I then manually forced the creation of the table by 1) backing up the database and renaming it. 2) In the python terminal running <app name>.db.create_all(). This then created all the tables as expected, including the Test model table. 3) I then manually deleted the Test model table in the database, re-ran flask db migrate but still no migration script was generated. I'm at a loss of what I'm doing wrong.

In my app's __init__.py file I import the models, db and migration by:

# Import flask and template operators
from flask import Flask, render_template, Blueprint
import os
# from flask_session import Session
# Import SQLAlchemy
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_uploads import UploadSet, configure_uploads
from flask_mail import Mail
# Define the WSGI application object
app = Flask(__name__)

# Configurations
app.config.from_pyfile(os.path.join('..','config.py'))

# Define the database object which is imported
# by modules and controllers
db = SQLAlchemy(app)
from . import models
migrate = Migrate(app, db)#, db,compare_type=True)

Any suggestions on what else I should try?

question

Most helpful comment

@miguelgrinberg

I believe I have solved the issue, although I'm still not sure what was the root cause. For others, here is what I did.

1) upgrade Flask-Migrate

pip install -U flask-migrate

2) moved the migrations folder

mv migrations/ migrations_old

3) re-executed flask db init
4) re-executed flask db migrate -> verified that the new migration script was correct
5) flask db upgrade then successfully upgraded the db

All 3 comments

Are you using the latest Flask-Migrate? I would expect to see more output, either a "no changes were detected" line or an error message.

Hi @miguelgrinberg, looks like I'm using version 2.2.1.

pip show flask-migrate
Name: Flask-Migrate
Version: 2.2.1
Summary: SQLAlchemy database migrations for Flask applications using Alembic
Home-page: http://github.com/miguelgrinberg/flask-migrate/
Author: Miguel Grinberg
Author-email: [email protected]
License: MIT
Location: /Users/<me>/anaconda3/envs/pt_org_app/lib/python3.6/site-packages
Requires: Flask, Flask-SQLAlchemy, alembic
Required-by:

edit: edited out name in path

@miguelgrinberg

I believe I have solved the issue, although I'm still not sure what was the root cause. For others, here is what I did.

1) upgrade Flask-Migrate

pip install -U flask-migrate

2) moved the migrations folder

mv migrations/ migrations_old

3) re-executed flask db init
4) re-executed flask db migrate -> verified that the new migration script was correct
5) flask db upgrade then successfully upgraded the db

Was this page helpful?
0 / 5 - 0 ratings