I think I get what you mean. In the mean time, another error popped
up:

Traceback (most recent call last):
  File "Main.py", line 32, in <module>
    MCS.addToTable()
  File "XXXXXXX/MonteCarloSimulation.py", line 138, in addToTable
####### <------- This function is described in the first post
    session.flush()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/
session.py", line 1354, in flush
    self._flush(objects)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/
session.py", line 1432, in _flush
    flush_context.execute()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/
unitofwork.py", line 261, in execute
    UOWExecutor().execute(self, tasks)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/
unitofwork.py", line 753, in execute
    self.execute_save_steps(trans, task)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/
unitofwork.py", line 768, in execute_save_steps
    self.save_objects(trans, task)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/
unitofwork.py", line 759, in save_objects
    task.mapper._save_obj(task.polymorphic_tosave_objects, trans)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/
mapper.py", line 1303, in _save_obj
    (state_str(state), instance_key, state_str(existing)))
sqlalchemy.orm.exc.FlushError: New instance <Student at 0x24fe7b0>
with identity key (<class 'ProjectParties.Student'>, (7713,))
conflicts with persistent instance <Student at 0x140f090>

What's going on here with the flush error?

I've gone back to using just copy.copy(some_dictionary) just >>after<<
I read into my dictionaries and >>before<< I start doing stuff like
monteCarloSimulation (above) and addToTable (above). Even if I stick
the copy.copy() after addToTable(), it still manages to give me the
same error.

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.

Reply via email to