Making changes to the schema
The final topic we will discuss in this chapter is how to make modifications to an existing Model definition. From the project specification, we know we would like to be able to save drafts of our blog entries. Right now we don't have any way to tell whether an entry is a draft or not, so we will need to add a column that lets us store the status of our entry. Unfortunately, while db.create_all()
works perfectly for creating tables, it will not automatically modify an existing table; to do this we need to use migrations.
Adding Flask-Migrate to our project
We will use Flask-Migrate to help us automatically update our database whenever we change the schema. In the blog virtualenv, install Flask-Migrate using pip
:
(blog) $ pip install flask-migrate
Note
The author of SQLAlchemy has a project called alembic; Flask-Migrate makes use of this and integrates it with Flask directly, making things easier.
Next we will add a Migrate
helper to our app. We will also create a...