Sorry, meant to reply to you Michael... ended up replying to myself! --- Firstly, apologies if I'm demanding too much but basically I'm quite a beginner at Python programming and this is for a University project, which is why I'm keen to get this done (due in a few days!). So I hope you won't mind me asking some questions that may seem really basic.
> deepcopy has issues because SQLAlchemy places extra information on your > objects, i.e. an _sa_instance_state attribute, that you dont want in your > copy. You *do* however need one to exist on your object. Therefore deepcopy > is not supported right now by SQLAlchemy ORM objects. > There are ways to manually blow away the old _sa_instance_state and put a new > one on the object, but the most straightforward is to make a new > object with __init__() and set up the attributes that are significant, > instead of doing a full deep copy. Could you explain what you mean by creating a new object with __init__() and setting up the attributes? Would this be a new class that isn't mapped using SQLA? > if you do really want to use deepcopy, you'd have to implement __deepcopy__() > on your objects and ensure that a new _sa_instance_state is set up, > there are functions in sqlalchemy.orm.attributes which can help with that. > This *should* be made an official SQLA recipe, but we haven't gotten > around to it. Could you please explain what you mean by that? Would it be possible to give me an idea or an example of how such would work? >> How can I stop it from closing the >> sessions? > nothing in SQLA closes sessions. Your program is doing that. I'm not issuing a session.close() anywhere (I checked). Are there any other ways of closing a session besides that? (If the answer is "Plenty", don't worry about it... I'll try to track it down then) On Jun 3, 7:41 pm, Michael Bayer <mike...@zzzcomputing.com> wrote: > On Jun 3, 2010, at 1:58 PM, Az wrote: > > > "Owning session has been closed"? Can I still use deepcopy if the > > session has not been closed? > > deepcopy has issues because SQLAlchemy places extra information on your > objects, i.e. an _sa_instance_state attribute, that you dont want in your > copy. You *do* however need one to exist on your object. Therefore deepcopy > is not supported right now by SQLAlchemy ORM objects. There are ways to > manually blow away the old _sa_instance_state and put a new one on the > object, but the most straightforward is to make a new object with __init__() > and set up the attributes that are significant, instead of doing a full deep > copy. > > if you do really want to use deepcopy, you'd have to implement __deepcopy__() > on your objects and ensure that a new _sa_instance_state is set up, there are > functions in sqlalchemy.orm.attributes which can help with that. This > *should* be made an official SQLA recipe, but we haven't gotten around to it. > > > How can I stop it from closing the > > sessions? > > nothing in SQLA closes sessions. Your program is doing that. > > > The problem is that if I change my shallow copied > > dictionary, the objects are changed. > > > Basically, I'm trying to do this state change thing where I'll take a > > dictionary (let's call it Node 1), make changes to it (thereby making > > changes to the objects it references) and then save those changes as > > Node 2. Then I'll take Node 2 and make some changes to that. So on and > > so forth for a certain number of changes. Everytime I do so, I want to > > retain the information from the previous Node as well as a "best node" > > which can be any of the Nodes. If my operations change the objects, is > > that even possible? > > > That was my motivation to use deepcopy but I don't want to stop using > > SQLAlchemy because of it :( > > > On Jun 3, 4:57 pm, Michael Bayer <mike...@zzzcomputing.com> wrote: > >> On Jun 3, 2010, at 1:24 AM, Az wrote: > > >>> +++ Questions +++ > > >>> 1. Is this the correct way to use sessions or am I sort of abusing > >>> them? > > >> I dont see any poor patterns of use above. > > >>> 2. When should I close a session? > > >> when you no longer need the usage of any of the objects associated with > >> it, or any remaining objects are in a state which you will re-merge them > >> into a new session before you next use them. The session in its default > >> state of autocommit=False is just like going to your database and starting > >> a transaction, doing some work - when you're done with the work, you close > >> the transaction, and all the data associated with that trans (i.e. your > >> ORM objects) is essentially "invalid"; other transactions can be modifying > >> that data. Your objects are an extension of the Session, which should be > >> considered as an object-oriented window onto a database transaction. > > >>> 3. I got the following error after trying to use copy.deepcopy() on > >>> one of my dictionaries. > > >>> "attribute refresh operation cannot proceed" % (state_str(state))) > >>> sqlalchemy.exc.UnboundExecutionError: Instance <Project at 0x24c5c50> > >>> is not bound to a Session; attribute refresh operation cannot proceed > > >> don't do deepcopy() on a structure that contains ORM objects if their > >> owning session has been closed. deepcopy on ORM objects probably has > >> issues that prevent it from working as you'd expect. You'd be better off > >> building copy constructors, i.e. def copy(self): return FooBar(....). > > > -- > > You received this message because you are subscribed to the Google Groups > > "sqlalchemy" group. > > To post to this group, send email to sqlalch...@googlegroups.com. > > To unsubscribe from this group, send email to > > sqlalchemy+unsubscr...@googlegroups.com. > > For more options, visit this group > > athttp://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 sqlalch...@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.