A developer attempts to access a lazy-loaded profile attribute after the session has closed. What error will occur, and why?
Python interview question for Advanced practice.
Answer
DetachedInstanceError. The session is closed, so the object cannot reach out to the DB to lazy-load the 'profile'.
Explanation
Objects are detached from the session once it's closed. Accessing unloaded attributes requires an active session, so SQLAlchemy raises DetachedInstanceError.