you can check for a given identity in the session using the identity key 
functions:

from sqlalchemy.orm.util import identity_key

key= identity_key(MyClass, someid)

object_already_in_session = key in session.identity_map

but usually people use session.merge(object) instead of session.add() to handle 
all of this automatically.



On Sep 19, 2011, at 4:55 AM, Eduardo wrote:

> Hi,
> I create a certain number of objects in a loop, I check if each of the
> objects is already stored in the session and if it is not I add it to
> the session.
> 
> session = Session()
> for item in items:
>   item1=create_item()
>   if not item1 in session:
>       session.add(item1)
> session.commit()
> 
> The problem is that when I add two objects to the session that are not
> the same but have some id I get the error:
> get an error sqlalchemy.exc.IntegrityError: (IntegrityError) duplicate
> key value violates unique constraint
> Is there any function that directly checks if there two objects with
> the same id in a session?
> Thanks
> 
> -- 
> 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.
> 

-- 
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