Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Migrations | Database
Flask Intensive Course: Web Development with Python

book
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.

python
from flask_migrate import Migrate

migrate = Migrate(app, db)

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.

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 5

Fragen Sie AI

expand
ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

We use cookies to make your experience better!
some-alt