Gaetan de Menten wrote: > On Sun, Oct 18, 2009 at 11:31, Jon Black <[email protected]> wrote: > >>> At first, I thought you were looking for session.new, session.dirty & >>> session.deleted >>> >> This could be what I need. If session.deleted contains everything that has >> been deleted, then I am getting the same result my performing the query on >> the session instead of the entity. >> >> >>> This is a FAQ in SQLAlchemy: >>> See >>> http://www.sqlalchemy.org/trac/wiki/FAQ#Imcallingdeletemyobjectanditisntremovedfromtheparentcollection >>> >>> So in your case that means: >>> >>> session.expire(user, ['addresses']) >>> >>> There are other options though, see the linked page. >>> >>> >> Is that really it? The article mentions using cascading, which I am already >> using. Doesn't that just mean that when I mark something to be deleted, it >> very nicely marks dependencies to be deleted? >> > > What that article implies is that you can have the instance both > deleted and removed from the list ("addresses" in this case), by using > cascades AND change your code to remove the instance from the list > instead of deleting it directly, as the cascade will "delete the > instance" automatically. So instead of doing > > address2.delete() > > you would do: > > user.addresses.remove(address2) >
That works great. I didn't realise that working on the collection would also update the session. That's good to know. Thanks. My problem now is probably unrelated to this, but I'll ask anyhow. If the user selects to delete an email address, I call user.addresses.remove(address1). Instead of pressing ok to confirm (where I'd call session.commit()), they cancel it. How can I undo that deletion? I can't do session.rollback because at this point, other changes may have been made to the user (for example, they edited their name), and it should still be possible to commit these changes. I think that I need transactions for this, but perhaps I'm wrong. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "SQLElixir" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sqlelixir?hl=en -~----------~----~----~----~------~----~------~--~---
