Contenido del Curso
Flask Intensive Course: Web Development with Python
Flask Intensive Course: Web Development with Python
Migrations
Let's imagine that we want to make some changes to our model. For instance, adding new fields (like 'dish_category'), removing the 'nullable=False' constraint from a field, or incorporating other constraints.
To implement changes in your Flask app's SQLAlchemy model to the database, you'll need to perform a migration.
Note
You don't need to implement this chapter in your project if you don't make any changes to the Recipe model. However, it's beneficial to understand this topic.
1. Install Flask-Migrate
2. Initialize Migration Directory
In the main.py add this two lines. The second one add after
db = SQLAlchemy(app)
line.
Then, initialize your migration directory with this command in the terminal:
3. Generate Migration Script:
Create an automatic migration script with:
Replace the text "made author non-nullable" with your meaningful text that describes the changes. This will generate a script in the migrations/versions directory. Review this script to make sure it correctly represents the changes you want to make (in our case, altering the description column to be non-nullable, as example).
Apply the Migration:
Finally, apply the migration to your database:
This will apply the changes to your database schema.
¡Gracias por tus comentarios!