

SQLite databases are the most convenient choice for developing small applications, sometimes even not so small ones, as each database is stored in a single file on disk and there is no need to run a database server like MySQL and PostgreSQL. The installation process for Flask-Migrate is similar to other extensions you have seen: (venv) $ pip install flask-migrateĭuring development, I'm going to use a SQLite database. Working with database migrations adds a bit of work to get a database started, but that is a small price to pay for a robust way to make changes to your database in the future. This extension is a Flask wrapper for Alembic, a database migration framework for SQLAlchemy. The second extension that I'm going to present in this chapter is Flask-Migrate, which is actually one created by yours truly. This is hard because relational databases are centered around structured data, so when the structure changes the data that is already in the database needs to be migrated to the modified structure. Most database tutorials I've seen cover creation and use of a database, but do not adequately address the problem of making updates to an existing database as the application needs change or grow. To install Flask-SQLAlchemy in your virtual environment, make sure you have activated it first, and then run: (venv) $ pip install flask-sqlalchemy This is extremely powerful, because you can do your development using a simple SQLite database that does not require a server, and then when the time comes to deploy the application on a production server you can choose a more robust MySQL or PostgreSQL server, without having to change your application. SQLAlchemy supports a long list of database engines, including the popular MySQL, PostgreSQL and SQLite. The nice thing about SQLAlchemy is that it is an ORM not for one, but for many relational databases. The job of the ORM is to translate the high-level operations into database commands. ORMs allow applications to manage a database using high-level entities such as classes, objects and methods instead of tables and SQL. The first is Flask-SQLAlchemy, an extension that provides a Flask-friendly wrapper to the popular SQLAlchemy package, which is an Object Relational Mapper or ORM. In this chapter I'm going to use two more. In Chapter 3 I showed you a first Flask extension. This application, like most others, can be implemented using either type of database, but for the reasons stated above, I'm going to go with a relational database. While there are great database products in both groups, my opinion is that relational databases are a better match for applications that have structured data such as lists of users, blog posts, etc., while NoSQL databases tend to be better for data that has a less defined structure. The latter group is often called NoSQL, indicating that they do not implement the popular relational query language SQL.

The databases can be separated into two big groups, those that follow the relational model, and those that do not. There are great choices for databases in Python, many of them with Flask extensions that make a better integration with the application.

This is one of the many areas in which Flask is intentionally not opinionated, which is great, because you have the freedom to choose the database that best fits your application instead of being forced to adapt to one. Databases in FlaskĪs I'm sure you have heard already, Flask does not support databases natively. The GitHub links for this chapter are: Browse, Zip, Diff. For most applications, there is going to be a need to maintain persistent data that can be retrieved efficiently, and this is exactly what databases are made for. The topic of this chapter is extremely important. Chapter 23: Application Programming Interfaces (APIs).Chapter 19: Deployment on Docker Containers.Chapter 15: A Better Application Structure.This is the fourth installment of the Flask Mega-Tutorial series, in which I'm going to tell you how to work with databases.įor your reference, below is a list of the articles in this series.
