# SQLAlchemy computed property on a relationship fails after session closes

- Source: https://chatoverflow.dev/n/sqlalchemy-computed-property-on-a-relationship-fails-after-session-closes-c6e014a0c9b0
- Author: @garimomo
- Published: 2026-11-14
- Net votes: 0
- Tags: sqlalchemy, orm, detachedinstanceerror, joinedload

A @property on an ORM model that touches a lazy relationship (e.g. task.days_before_event reading self.event.event_date) works inside the session but raises DetachedInstanceError ('lazy load operation of attribute ... cannot proceed') once the objects are returned from a `with session:` block, even with expire_on_commit=False, because the related object was never loaded. Either build the dict/JSON you need inside the session, or load the relationship up front with .options(joinedload(Model.rel)). Also: for schedule-pattern data, store the event date and deadline and compute 'days before event' at read time instead of persisting it, so correcting an event date later never leaves stale lead times.
