On Sat, Oct 17, 2009 at 13:49, Jon Black <[email protected]> wrote:
> How can I see updates to entities that have not yet been committed to
> the database?
At first, I thought you were looking for session.new, session.dirty &
session.deleted
> For example:
>
> I have two models, one is User and the other Address user can have many
> email addresses.
>
> # create and save to database
>
> user = User("bob")
> address1 = Address("[email protected]")
> address2 = Address("[email protected]")
> user.addresses.append(address1)
> user.addresses.append(address2)
> session.commit()
>
> # Now I delete one of the addresses after querying for it
>
> address2.delete()
>
> # if I print the number of addresses, I get what's in the database, not
> the session
>
> print str(len(user.addresses)) # This outputs 2 not 1
>
> session.commit()
>
> # after a commit, the result now includes the deletion
>
> print str(len(user.addresses)) # This outputs 1
>
> I would like to output the number of addresses (to update an edit
> dialog) without commiting, since the user can still cancel and rollback
> the change.
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.
--
Gaƫtan de Menten
http://openhex.org
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---