Insertion

In this lesson, we will learn how to populate our database by inserting data.

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 = db.Column(db.String, primary_key=True, unique=True, nullable=False)
    password = db.Column(db.String, nullable=False)

1. Create a new User object #

To create a new user we will simply create an object of the User class.

new_user = User(email = "archie.andrews@email.com", password = "football4life")

Get hands-on with 1200+ tech skills courses.