What's the use of expunge?

1,069 views
Skip to first unread message

一首诗

unread,
Feb 26, 2009, 1:29:00 AM2/26/09
to sqlalchemy
The document says:

====================================================
Expunge removes an object from the Session, sending persistent
instances to the detached state, and pending instances to the
transient state:

====================================================

I hoped that if an instance was expunged from a session, I might use
it safely as a container of some temp data without fearing trigger any
database query.

But I found that if an object is 'expunge'd , it's properties will
not be accessible anymore.

So, when should we use expunge? Or it is kept for sqlalchemy inner
use only?

Michael Bayer

unread,
Feb 26, 2009, 7:54:17 AM2/26/09
to sqlal...@googlegroups.com

you can reattach the object to a session later if you need it to be
part of a transaction again. it can be used for caching scenarios,
for example. But I agree most reasonable applications probably won't
use expunge() very much.


Wichert Akkerman

unread,
Feb 26, 2009, 8:21:01 AM2/26/09
to sqlal...@googlegroups.com

What happens if you do not call expunge on it, but pickle the object in a
cache, load it later and then merge it?

Wichert.

--
Wichert Akkerman <wic...@wiggy.net> It is simple to make things.
http://www.wiggy.net/ It is hard to make things simple.

Michael Bayer

unread,
Feb 26, 2009, 10:04:47 AM2/26/09
to sqlal...@googlegroups.com

On Feb 26, 2009, at 8:21 AM, Wichert Akkerman wrote:

>
> What happens if you do not call expunge on it, but pickle the object
> in a
> cache, load it later and then merge it?

the state of the newly unpickled object, that is the current value of
its mapped attributes, would be merged with the persistent version in
the session. merge() will load the object from the database into an
in-session, persistent instance before merging the external state.
because your unpickled instance never actually enters the session,
conflicts with its previous session or an already present in-session
object are nicely avoided.

Wichert Akkerman

unread,
Feb 26, 2009, 10:27:10 AM2/26/09
to sqlal...@googlegroups.com

I actually skip that and invalidate the cache entry on changes to
prevent that SQL hit. What I meant was: does it matter if you never
explicitly call expunge?

Michael Bayer

unread,
Feb 26, 2009, 10:35:52 AM2/26/09
to sqlal...@googlegroups.com

On Feb 26, 2009, at 10:27 AM, Wichert Akkerman wrote:

>
> Previously Michael Bayer wrote:
>> On Feb 26, 2009, at 8:21 AM, Wichert Akkerman wrote:
>>> What happens if you do not call expunge on it, but pickle the object
>>> in a
>>> cache, load it later and then merge it?
>>
>> the state of the newly unpickled object, that is the current value of
>> its mapped attributes, would be merged with the persistent version in
>> the session. merge() will load the object from the database into an
>> in-session, persistent instance before merging the external state.
>> because your unpickled instance never actually enters the session,
>> conflicts with its previous session or an already present in-session
>> object are nicely avoided.
>
> I actually skip that and invalidate the cache entry on changes to
> prevent that SQL hit. What I meant was: does it matter if you never
> explicitly call expunge?

for pickling ? not at all.

Reply all
Reply to author
Forward
0 new messages