How to query database by id using SqlAlchemy?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Querying a row by its primary key is one of the most common ORM operations, and SQLAlchemy gives it a dedicated API for a reason. If you know the record's id, the modern answer is usually Session.get(), not a generic filtered query.
The Modern Way: Session.get()
In current SQLAlchemy ORM usage, Session.get() is the primary-key lookup method. It returns the mapped object if found, or None if the row does not exist.
This is the clearest option when you are retrieving by primary key and only need one row.
Why Session.get() Is Preferred
SQLAlchemy's session maintains an identity map for ORM objects keyed by primary key. Because of that, Session.get() can return an already-loaded object directly when it is present in the session, instead of always constructing a new SELECT path the way a generic query does.
That makes the method both semantically correct and potentially more efficient. It also communicates intent clearly to anyone reading the code: this is a primary-key fetch, not an arbitrary filter.
Older Query Style Versus Current Style
You may still see older examples like this:
That pattern belongs to the legacy query API. The equivalent modern style is:
If you are writing new code, use Session.get().
Querying by a Different Column
If the value you have is not the primary key, then Session.get() is not the right tool. In that case, use select() and a filter condition.
This distinction matters. Session.get() is strictly for primary key identity, while select() handles general queries.
Composite Primary Keys
If the mapped table has a composite primary key, pass a tuple or dictionary of primary-key values.
The ordering in the tuple follows the mapped primary-key column order.
Handling Missing Rows Cleanly
Because Session.get() returns None when nothing matches, the usual handling pattern is straightforward:
This is cleaner than forcing a list result and checking whether the list is empty.
A Reusable Helper Function
If you query by id in several places, wrapping the pattern is reasonable:
The helper remains simple because Session.get() already matches the use case precisely.
Common Pitfalls
One common mistake is using .all() for a primary-key lookup. That returns a list, adds unnecessary work, and makes the result shape harder to reason about.
Another mistake is using Session.get() for non-primary-key columns. If you only know an email address or username, switch to select() with a filter.
A third pitfall is copying old examples that use query.get(). They may still appear in older tutorials, but the current ORM API centers on Session.get().
Session scope is another source of confusion. Session.get() can reuse an object already present in the session's identity map, but that benefit only exists within the same session lifecycle.
Summary
- Use
session.get(Model, primary_key)for primary-key lookups in modern SQLAlchemy - '
Session.get()returns one mapped object orNone' - Prefer
select()with filters when the lookup column is not the primary key - Legacy
query.get()examples should be updated toSession.get() - Composite primary keys can be passed as tuples or dictionaries

