5

I'm developing a web application based on Flask + SqlAlchemy + MySQL and I followed that answer. I have to change my model so I need to alter the db table adding a column, but I don't want to lose the stored data dropping and recreating it.

I read something about Flask-Migrate and Alembic... are they essential to solve my problem? What can I do?

Community
  • 1
  • 1
rh0x
  • 1,066
  • 1
  • 13
  • 33

3 Answers3

5

Yes, they can be helpful. I would suggest Alembic (http://alembic.readthedocs.org/en/latest/) which is from the author of SQLAlchemy.

With migration you need to define the changes you want to make. In your case, you want to add a column. I don't think it would be a problem. Adding columns usually don't cause data loss. Changing type or deleting a column causes data loss.

You can of course directly update the table too, just update your models to reflect the new column. But migrations are usually more manageable ways of doing it.

masnun
  • 11,635
  • 4
  • 39
  • 50
  • 1
    I have configured alembic using the docs in 10 minutes: the upgrade worked very well! thank you – rh0x Dec 17 '15 at 10:50
0

A simple solution would be to ALTER TABLE in your SQL and then changing the value in your table.

Varun Rajkumar
  • 374
  • 1
  • 4
  • 13
0

Flask-Migrate

Flask-Migrate is an extension that handles SQLAlchemy database migrations for Flask applications using Alembic. The database operations are made available through the Flask command-line interface.

>>> pip install flask-migrate

https://flask-migrate.readthedocs.io/en/latest/index.html

fedotsoldier
  • 190
  • 1
  • 16
  • Flask-Migrate requires Flask-SQLAlchemy, not straight up SQLAlchemy. [See](https://stackoverflow.com/a/70725094/7623145) – user2003052 Jan 24 '22 at 10:06