Databases in Flask
Learn how to add more functionality to your Flask apps with databases!
StartIntroduction to Flask-SQLAlchemy
Lesson 1 of 2
- 1Web applications are often built around a lot of data that change frequently. The data is usually organized in entities related in some way. For example, entities such as users are related to produ…
- 2Flask-SQLAlchemy is an extension for Flask that supports the use of a Python SQL Toolkit called SQLAlchemy. To start creating a minimal application, in addition to importing Flask, we also need…
- 3The database object db created in our application contains all the functions and helpers from both SQLAlchemy and SQLAlchemy Object Relational Mapper (ORM). SQLAlchemy ORM associates user-defined …
- 4Adding another model or table schema to your application is simple. You only need to create another class that inherits from Model. The model you will create next, Reader, is simple and similar t…
- 5Often times in real-world applications we will have entities that are somehow related. Students take courses, customers buy products, and users comment on posts. In SQLAlchemy we can declare a *re…
- 6In the previous lesson, we began adding a one-to-many relationship to the Book and Reader models by using .relationship(). But that does not completely specify our one-to-many relationship. We addi…
- 7When you ran your application in the previous exercises you might have realized that there is no database file created in the application folder. The reason is simple: we need to explicitly initial…
- 8Now that we initialized our database schema, the next step is to start creating entries that will later populate the database. The beauty of SQLAlchemy Object Relational Mapper (ORM) is that our da…
- 9Creating objects for tables that have foreign keys is not much different from the usual creation of Python objects. Consider that we have the following objects already created: b1 = Book(id = 12…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory