tags:

views:

13

answers:

1

This: Intro to object states lists the four permutations of presence-in-DB/presence-in-session:

transient, pending, persistent & detached

Is there any way of querying a given object to return which of the four states the object is in?

I tried rooting around in _sa_instance_state but couldn't find anything relevant.

Thanks!

+1  A: 

Fount it here:

from sqlalchemy.orm import object_session 
from sqlalchemy.orm.util import has_identity 

# transient: 
object_session(obj) is None and not has_identity(obj) 
# pending: 
object_session(obj) is not None and not has_identity(obj) 
# detached: 
object_session(obj) is None and has_identity(obj) 
# persistent: 
object_session(obj) is not None and has_identity(obj) 
EoghanM