On Oct 17, 2012, at 9:55 PM, Kent wrote:

> The attached script fails with "sqlalchemy.exc.InvalidRequestError: Instance 
> '<Bug at 0x1e6f3d10>' has been deleted.  Use the make_transient() function to 
> send this object back to the transient state."
> 
> While this example is somewhat convoluted, I have a few questions about 
> sqlalchemy behavior here:
> 
> 1) At the session.flush(), even though the Rock and the bugs relationship 
> have been expunged, the pending delete still is issued to the database.  
> Would you expect/intend sqlalchemy to delete even after the expunge()?

no, because the Rock you have merged has established that the Bug is no longer 
associated with it.   You expunge the Rock, there's no Bug attached to it to be 
expunged.

> 
> 2) After the flush(), shouldn't the history of the 'bugs' relationship have 
> been updated to reflect the statement issued to the database?  (See print 
> statement)

yes, because merged is not in that Session anymore.    The flush() is what 
resets the history.  Clearly it's not going to go out to find objects that 
aren't in that Session.

> 
> 3) The InvalidRequestError is only raised if the 'bugs' relationship has a 
> backref, otherwise it isn't raised.  Any idea why?

removing the Bug.rock path means that when you expunge "merged", there is 
nothing left in the flush process to handle the orphan cascade you're looking 
for here.   The DELETE does not occur so the Bug that's present on merged can 
go right back in.

> 
> 4) Don't hate me for asking: is there a work around?  I'm trying to 
> understand this scenario since in a rare case, it presents.

The workaround is don't use expunge().    This is not a method I ever use for 
anything, actually.     And especially, if you manipulate the state within the 
Session, then expunge() random segments of that state, you can hardly expect 
flush() to have a clear idea of what you intend.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to