Seleccionar página

You successfully migrated your database and populated the products table with three items. We will modify the database schema and add a new price column to the products table, then migrate and upgrade our database while preserving this data using Flask-Migrate. The migration script needs to be reviewed and edited, as Alembic is not always able to detect every change you make to your models.

Flask-Migrate is a python package that configures SQLAlchemy and Alembic for your Flask application. Alembic is the migration tool that actually takes care of database migrations under the hood. It can automatically generate migration files based on database schema models, and apply those migrations to databases. This is truly powerful, since it enables you to have a single source of truth and a history of your database changes in source control.

Written by Python Code Nemesis

To sync the database in another system just refresh the migrations folder from source control and run the upgrade command. This tutorial provides a basic understanding of how Flask can be used to build a web application that interacts with a database and how to handle database migrations. It can be a starting point for more complex applications that require user authentication, complex queries, or integration with other services. Let’s edit the migration file to add create table and drop table operation to the upgrade() and downgrade() function respectively.

The base target causes all migrations to be downgraded, until the database is left at its initial state, with no tables. The model class created in the previous section defines the initial database structure (or schema) for this application. But as the application continues to grow, it is likely that I will need to make changes to that structure such as adding new things, and sometimes to modify or remove items. Alembic (the migration framework used by Flask-Migrate) will make these schema changes in a way that does not require the database to be recreated from scratch every time a change is made.

Sponsor this project

These are the actual commands we will be using while performing database migrations. While developing application it is common to change table schema. A tool like Alembic flask developer allows us to change database schema as our application evolves. It also keeps track of the changes made to the database, so that you can move forward or backward in time.

  • ORMs allow applications to manage a database using high-level entities such as classes, objects and methods instead of tables and SQL.
  • A type declaration such as so.Mapped[int] or so.Mapped[str] define the type of the column, and also make values required, or non-nullable in database terms.
  • You’ll note in each migration script there is information about the previous migration and the next migration.
  • You will find that it has two functions called upgrade() and downgrade().
  • The User class has a new posts field, that is initialized with so.relationship().
  • In this case, db upgrade would stop after running the specified migration and will not proceed to run the latest migration.

In the downgrade() function, we are using drop_table() directive which issues DROP TABLE statement. Alembic (through Flask-Migrate) creates new tables when they do not exist. The SQLAlchemy documentation is the best place to learn about the many options that are available to query the database. The first argument to so.relationship() is the model class that represents the other side of the relationship.

Updating Additional Databases

Note that the id fields were automatically set to 1 and 2 when those users were added. This happens because SQLAlchemy configures integer primary key columns to be auto-incrementing. The application is in its infancy at this point, but it does not hurt to discuss what is going to be the database migration strategy going forward.

flask migration

Changes to a database are done in the context of a database session, which can be accessed as db.session. Multiple changes can be accumulated in a session and once all the changes have been registered you can issue a single db.session.commit(), which writes all the changes atomically. If at any time while working on a session https://remotemode.net/ there is an error, a call to db.session.rollback() will abort the session and remove any changes stored in it. The important thing to remember is that changes are only written to the database when a commit is issued with db.session.commit(). Sessions guarantee that the database will never be left in an inconsistent state.

Abrir chat
1
Escanea el código
Hola
¿En qué podemos ayudarte?