Update and delete are also basic operations that are performed on a database. So, let’s find out how we perform these operations using the SQLAlchemy ORM.

Update #

The method for updating a record is very straightforward, and you do not have to remember any new commands. It can be achieved in three easy steps.

1. Retrieve the object to update #

First, to update any record that is already present in the database, we must first retrieve it. We have already learned how to do the retrieval in the last lesson. So let us retrieve one record with respect to its primary_key.

user = User.query.get("veronica.lodge@email.com")

2. Update the object #

Afterward, we can update the value inside the retrieved object.

user.email = "veronica@email.com"

3. Commit the changes in session #

Now, we commit these changes so that they become persistent in the database.

db.session.commit()

Complete example for update #

Get hands-on with 1200+ tech skills courses.