Flask-Migrate not creating tables

痴心易碎 提交于 2019-11-30 08:09:56

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.

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()
matchew

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.

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.

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

Strange solve for me is: delete database and folder migrations. Then

>>> from app import db
>>> db.create_all()

After flask db init or python app.py db init and then flask db migrate or python app.py db migrate. Wow, It's strange, but it works for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!