How to update SQLAlchemy row entry?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
To update a row in SQLAlchemy, query the object, modify its attributes, and call session.commit(). For bulk updates, use session.query(Model).filter(...).update({...}) which generates a single UPDATE statement without loading objects into Python. SQLAlchemy supports both the ORM pattern (load-modify-commit) and the Core pattern (direct SQL expressions). The ORM approach is simpler for single-row updates; the Core/bulk approach is faster for updating many rows at once.
ORM Update: Query, Modify, Commit
SQLAlchemy tracks changes to loaded objects automatically. When you modify user.name, the session marks the object as "dirty." commit() generates and executes the UPDATE statement.
Using session.get() for Primary Key Lookup
session.get() checks the identity map (in-memory cache) before hitting the database, making it faster than query().filter_by() when you have the primary key.
Bulk Update with filter().update()
filter().update() generates a single SQL statement: UPDATE users SET email='[email protected]' WHERE age > 30. No Python objects are loaded.
SQLAlchemy 2.0 Style Updates
Upsert (Insert or Update)
For PostgreSQL, use from sqlalchemy.dialects.postgresql import insert. The dialect-specific on_conflict_do_update generates an INSERT ... ON CONFLICT DO UPDATE statement.
Update with Relationships
Common Pitfalls
- Forgetting to call
session.commit(): SQLAlchemy does not auto-commit. Modified objects remain "dirty" in the session until you explicitly callcommit(). Without it, no SQL is sent to the database and changes are lost when the session closes. - Using
synchronize_session=Falsewithout understanding the consequence:filter().update(synchronize_session=False)does not update objects already loaded in the session. If you later access those objects, they still have stale values. Usesynchronize_session='evaluate'(default) or'fetch'to keep the session in sync, or expire all withsession.expire_all(). - Catching an exception without rolling back: If an error occurs after modifying objects but before committing, the session is in an inconsistent state. Always use
session.rollback()in exception handlers, or usewith Session(engine) as session:which auto-rolls-back on exceptions. - Updating a detached object: Objects loaded in one session are "detached" after that session closes. Modifying a detached object and committing in a new session raises
DetachedInstanceError. Usesession.merge(obj)to re-attach an object to a new session before updating. - Bulk update bypassing ORM events and hooks:
filter().update()and theupdate()construct execute SQL directly, skippingbefore_updateevents, validation hooks, and__setattr__overrides. If your model relies on ORM-level hooks, use the load-modify-commit pattern instead.
Summary
- Load an object with
session.get()orsession.query(), modify its attributes, and callsession.commit() - Use
filter().update({...})for efficient bulk updates that generate a single SQL statement - Use SQLAlchemy 2.0
update()construct for type-safe, composable bulk updates - Always commit after changes and use
session.rollback()on errors - Choose between ORM updates (triggers hooks, loads objects) and Core updates (fast, skips hooks) based on your needs
Related reading
- How to update the _id of one MongoDB Document?
- How to update values using pymongo?
- How to upgrade AWS RDS Aurora MySQL 5.6 to 5.7
- How to upload and retrieve file in mongodb in spring boot application without using GridFSTemplate?
- How to update/upgrade a package using pip?
- How to update/upgrade a package using pip?
- How to use 2 or more databases with spring?
- How to use aggregate functions in Amazon Dynamodb

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.