Skip to content Skip to sidebar Skip to footer

Flask-migrate Not Creating Tables

I have the following models in file listpull/models.py: from datetime import datetime from listpull import db class Job(db.Model): id = db.Column(db.Integer, primary_key=Tru

Solution 1:

Ensure to import the Models in the manage.py file (or the file with the migrate instance). You have to import the models in the file, even if you are not explicitly using them. Alembic needs these imports to migrate, and to create the tables in the database. For example:

# ... some imports ...from api.models import User, Bucketlist, BucketlistItem # Import the models

app = create_app('dev')
manager = Manager(app)
migrate = Migrate(app, db)

manager.add_command('db', MigrateCommand)

# ... some more code here ...if __name__ == "__main__":
    manager.run()
    db.create_all()

Solution 2:

When you call the migrate command Flask-Migrate (or actually Alembic underneath it) will look at your models.py and compare that to what's actually in your database.

The fact that you've got an empty migration script suggests you have updated your database to match your model through another method that is outside of Flask-Migrate's control, maybe by calling Flask-SQLAlchemy's db.create_all().

If you don't have any valuable data in your database, then open a Python shell and call db.drop_all() to empty it, then try the auto migration again.

UPDATE: I installed your project here and confirmed that migrations are working fine for me:

(venv)[miguel@miguel-linux nhs-listpull]$ ./run.py db init
  Creating directory /home/miguel/tmp/mark/nhs-listpull/migrations...done
  Creating directory /home/miguel/tmp/mark/nhs-listpull/migrations/versions...done
  Generating /home/miguel/tmp/mark/nhs-listpull/migrations/script.py.mako...done
  Generating /home/miguel/tmp/mark/nhs-listpull/migrations/env.pyc...done
  Generating /home/miguel/tmp/mark/nhs-listpull/migrations/env.py...done
  Generating /home/miguel/tmp/mark/nhs-listpull/migrations/README...done
  Generating /home/miguel/tmp/mark/nhs-listpull/migrations/alembic.ini...done
  Please edit configuration/connection/logging settings in'/home/miguel/tmp/mark/nhs-listpull/migrations/alembic.ini' before
  proceeding.
(venv)[miguel@miguel-linux nhs-listpull]$ ./run.py db migrate
INFO  [alembic.migration] Context impl SQLiteImpl.
INFO  [alembic.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate] Detected added table 'list_type'
INFO  [alembic.autogenerate] Detected added table 'job'
  Generating /home/miguel/tmp/mark/nhs-
  listpull/migrations/versions/48ff3456cfd3_.py...done

Try a fresh checkout, I think your setup is correct.

Solution 3:

I had the same issue but a different problem caused it. Flask-migrate workflow consists of two consequent commands:

flask db migrate

which generates the migration and

flask db upgrade

which applies the migration. I forgot to run the last one and tried to start next migration without applying the previous one.

Solution 4:

For anyone coming who comes across this, my problem was having

db.create_all()

in my main flask application file which created the new table without the knowledge of alembic

Simply comment it out or delete it altogether so it doesn't mess with future migrations.

but unlike @Miguel's suggestion, instead of dropping the whole database (i had important information in it), i was able to fix it by deleting the new table created by Flask SQLAlchemy and then running the migration.

and this time alembic detected the new table and created a proper migration script

Solution 5:

I just encountered a similar problem. I'd like to share my solution for anyone else encountering this thread. For me, I had my models in a package. For example models/user.py and I tried from app.models import * which did not detect anything on the migrate. However, if I changed the import to from app.models import user this is okay why my project is young, but as I have more models a bulk import would be preferable.

Post a Comment for "Flask-migrate Not Creating Tables"