Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unbind object from session

Is it possible to unbind an object from an SQLAlchemy session?

I used to deepcopy it, but as this seems not to be possible when using association proxies I'm searching for another solution to remove the object from a session to add it to another one.

like image 931
Manuel Faux Avatar asked Jun 26 '12 18:06

Manuel Faux


1 Answers

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

session.expunge(obj1)

UPDATE: A detached object can be attached to the same or another session by using

session2.add(obj1)

or merged

session2.merge(obj1)

With merge(), the given instance is not placed within the session, and can be associated with a different session or detached. merge() is very useful for taking the state of any kind of object structure without regard for its origins or current session associations and placing that state within a session.

like image 112
Maksym Polshcha Avatar answered Nov 04 '22 11:11

Maksym Polshcha