debugging DetachedInstanceError

115 views
Skip to first unread message

Jonathan Vanasco

unread,
Apr 22, 2020, 8:57:32 PM4/22/20
to sqlalchemy
I'm trying to figure out how I got a DetatchedInstanceError


DetachedInstanceError: Parent instance <AcmeAuthorization at 0x107a62b90> is not bound to a Session; lazy load operation of attribute 'domain' cannot proceed (Background on this error at: http://sqlalche.me/e/bhk3)


This is happening in a web application where I never call `dbSession.close()`. It does use zope.sqlalchemy for pyramid_tm, but `keep_session` is True.  I set `expire_on_commit=False` on the sessionmaker.

Any tips on the best ways to troubleshoot how/where this is getting removed from a session?



Mike Bayer

unread,
Apr 23, 2020, 10:17:12 AM4/23/20
to noreply-spamdigest via sqlalchemy
Using the event hook is likely the most direct way to see where it's happening, either via logging or pdb:

--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Mike Bayer

unread,
Apr 23, 2020, 10:17:58 AM4/23/20
to noreply-spamdigest via sqlalchemy
also the item can be detached if it was deleted in a previous flush

On Wed, Apr 22, 2020, at 8:57 PM, Jonathan Vanasco wrote:

Jonathan Vanasco

unread,
Apr 23, 2020, 5:20:51 PM4/23/20
to sqlalchemy


On Thursday, April 23, 2020 at 10:17:12 AM UTC-4, Mike Bayer wrote:
Using the event hook is likely the most direct way to see where it's happening, either via logging or pdb:


 
Awesome! Thanks mike. That's exactly what I needed.

After several hours of debugging, I finally tracked down the issue - which solved at least 4 outstanding bugs, and let me clear out a bunch of code.  And now I can have `expire_on_commit` back too!

I never realized that `zope.sqlalchemy.mark_changed(` has it's own `keep_session` kwarg... which defaults to `False` and does not inherit from the existing configuration. 

I can't tell why the errors on this was inconsistent.  The app has a simplified "commit" hook that lets me handle multiple `pyramid_tm` commits in a single request, and I invoke mark_changed() in there to pick up a few edge cases.  In a small number of these instances, I lost the `keep_session` setting - in others I didn't.  








Reply all
Reply to author
Forward
0 new messages