Python & FastAPI•Part 5: ORMs, Databases & Background Workers (56 - 65)
Unit of Work and Identity Map:
In Simple Words:
- Identity Map: Tracks all loaded objects by primary key, ensuring querying the same row twice returns the exact same in-memory object instance.
- Unit of Work: Batches all inserts, updates, and deletes, executing them in optimal order upon
session.commit().
python
# Identity Map in action:
user1 = session.get(User, 1)
user2 = session.get(User, 1)
assert user1 is user2 # Same memory address!