Search⌘ K
AI Features

Insertion

Explore the process of inserting data into a database within a Flask application using SQLAlchemy models. Understand creating model instances, managing database sessions, committing changes, and handling exceptions to ensure data integrity and persistence.

In the last chapter, we figured out how to connect to a database with the Flask application. Furthermore, we learned how to create Models and relationships between these models. In this chapter, we will be focusing on all kinds of operations that we can perform on our models and the changes that will occur in the database. The type of operations that we will need to make are:

  1. Insert
  2. Delete
  3. Retrieve
  4. Update

Introduction #

First, we need to populate our database with the data. Let’s insert some data using the User model that we created in the last chapter.

class User(db.Model):
    email
...