> Expected: students, supervisors, projects, dictionaries of said objects,
> and other attribute values (strings, ints, lists, etc.). Unexpected:
> anything else, especially sessions, InstanceState objects, or other ORM
> support objects.

Actually got some stuff like the following (copy-pasting bits from my
print output):

(<class 'sqlalchemy.orm.state.InstanceState'>,)
{'_sa_instance_state': <sqlalchemy.orm.state.InstanceState object at
0x2d5beb0>, 'proj_id': 1100034, 'postsim_probs': [], 'proj_sup': 1291,
'presim_pop': 0, 'own_project': False, 'allocated': False,
'proj_name': 'MPC on a Chip', 'blocked': False}

Stuff like that :S

> Calling session.close() is not necessary if you have a single global
> session like you do. You only need it if you are worried that the
> database might get modified concurrently by another transaction (from a
> different process, session, etc.). Having said this, session.close()
> does not prevent you from using the session later on: it just closes out
> any pending transaction and expunges all object instances (including any
> student, supervisor, and project instances you may have added/loaded).
> This ensures that it sees fresh data for any future queries.
>
> In conclusion, using session.query(Student)... should work whether you
> have run monteCarloBasic or not.

Excellent :)

> The most likely cause is if you call session.add(temp_alloc) after
> calling session.merge(temp_alloc) for the same temp_alloc object. I
> noticed your original monteCarloBasic had two calls to
> session.add(temp_alloc); did both get changed to
> session.merge(temp_alloc)? If that doesn't work, can you verify that
> SQLAlchemy's primary key for SimAllocation matches the database's
> primary key for sim_alloc? What column type are you using for uid? Which
> call to session.merge is failing (line 163 according to your traceback),
> the one inside your "for rank in ranks" loop or the one outside?

Oh yeah good point, they're separate calls. Basically for the one in
"for rank in ranks"
adds for a student getting a project, the other adds if a student
doesn't get a project since we want
to track all students (allocated or not, since the state of being
unallocated is what gives
us motivation to optimise the results).

Anyway, session.merge() is for overwriting previously existing values
right? Now thanks to the UUID I can add multiple calls to
monteCarloBasic() to my physical database :)

I basically wrote a small function that, for everytime the
monteCarloBasic() is called, will append the UUID, the number of
trials ran and the date-time to a text file. My supervisor would have
to copy paste that into a GUI text field or the command line but it's
not that much of a hassle, given the usefulness of the database.

On Jun 9, 4:25 pm, Conor <conor.edward.da...@gmail.com> wrote:
> On 06/09/2010 12:44 AM, Az wrote:
>
>
>
> > Traceback (most recent call last):
> >   File "Main.py", line 39, in <module>
> >     MCS.monteCarloBasic(trials)
> >   File "/XXXX/MonteCarloSimulation.py", line 163, in monteCarloBasic
> >     session.merge(temp_alloc)
> >   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 1158, in merge
> >     self._autoflush()
> >   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 897, in _autoflush
> >     self.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 1428, in _save_obj
> >     c = connection.execute(statement.values(value_params), params)
> >   File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
> > lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/
> > engine/base.py", line 824, in execute
> >     return Connection.executors[c](self, object, multiparams, params)
> >   File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
> > lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/
> > engine/base.py", line 874, in _execute_clauseelement
> >     return self.__execute_context(context)
> >   File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
> > lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/
> > engine/base.py", line 896, in __execute_context
> >     self._cursor_execute(context.cursor, context.statement,
> > context.parameters[0], context=context)
> >   File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
> > lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/
> > engine/base.py", line 950, in _cursor_execute
> >     self._handle_dbapi_exception(e, statement, parameters, cursor,
> > context)
> >   File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
> > lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/
> > engine/base.py", line 931, in _handle_dbapi_exception
> >     raise exc.DBAPIError.instance(statement, parameters, e,
> > connection_invalidated=is_disconnect)
> > sqlalchemy.exc.IntegrityError: (IntegrityError) columns uid,
> > session_id, stud_id are not unique u'INSERT INTO sim_alloc (ident,
> > uid, session_id, stud_id, alloc_proj, alloc_proj_rank) VALUES
> > (?, ?, ?, ?, ?, ?)' [1, '1d295f48-7386-11df-8e87-00264a052efc', 1,
> > 5796, 1100009, 1]
>
> > ########
>
> > Good news: Got the UUID working in a snap.
> > Bad news: See error :(
>
> > Note: This happened when I started using 'session.merge(temp_alloc)'
> > instead of 'session.add'
>
> The most likely cause is if you call session.add(temp_alloc) after
> calling session.merge(temp_alloc) for the same temp_alloc object. I
> noticed your original monteCarloBasic had two calls to
> session.add(temp_alloc); did both get changed to
> session.merge(temp_alloc)? If that doesn't work, can you verify that
> SQLAlchemy's primary key for SimAllocation matches the database's
> primary key for sim_alloc? What column type are you using for uid? Which
> call to session.merge is failing (line 163 according to your traceback),
> the one inside your "for rank in ranks" loop or the one outside?
>
> Also, since you know you are creating new sim_alloc rows in the database
> (instead of overwriting existing ones), you can use session.add instead
> of session.merge. This will prevent unnecessary SELECTs to your database.
>
> -Conor

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