python

Update column values query in SQLAlchemy

You can update column values of a row in SQLAlchemy by using the .update() method and assigning new values to its model properties.

session.query(
    UserModel
).filter(
    UserModel.id == 20
).update({
    UserModel.username: 'john_deo',
    UserModel.email: '[email protected]',
    UserModel.city: 'New York'
})

session.commit()

In the code snippet, we are updating the User's email, username, the city which has id 20.

Was this helpful?