2

I have a Flask project with MySQL database and uses SQLAlchemy as ORM and Flask-Migrate*for migrations.

I wrote my models and when I run the migrations, migration file is empty because existing tables are out of Flask-Migrate's control so I actually have to delete those tables to let migration tool to detect and create them again. But the problem is that I do not want to delete and create my tables.

So is there a way that I can sync my model with my existing table ?

EDIT : I just found out that in env.py file, it's possible to specify tables that exists and it will not create those tables :

metadata.reflect(engine, only=["table1", "table2"])

Thanks for the answer.

oyayla
  • 21
  • 3

1 Answers1

2

Automatic migrations are by definition generated as a delta between your models and your database. If you already have a database that was created before you started using Flask-Migrate/Alembic then you can begin tracking migrations from that point on.

If you want to generate an initial migration that takes you to your current version the easiest way is to delete all the tables, as you suggested. To avoid losing your data I can suggest two ideas:

  • backup your database before deleting your tables, then restore after the migration was generated.
  • temporarily point your application at an empty database (a different one). Once the migration is generated point it back to your database.

I hope this helps.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152