In my code, I am currently adding to the "session" in various modules (this is the same session since I'm importing it from my most prominent module).
Some sample code would be: ###### BEGIN CODE 1 ####### Session = sessionmaker(bind=engine) session = Session() def addToTable(): """Very simple SQLAlchemy function that populates the Student, Project and Supervisor tables.""" for student in students.itervalues(): session.add(student) session.flush() for project in projects.itervalues(): session.add(project) session.flush() for supervisor in supervisors.itervalues(): session.add(supervisor) session.flush() session.commit() And then again in a function in the same module: def monteCarloBasic(trials): """The Monte-Carlo simulation will generate allocations for the list of students by randomly arranging the order for each trial. In the case of a student having more than one project for a given rank, the algorithm with randomly select one of them since it is given that all such projects are equally desireable to the student.""" session_id = 1 ident = 1 for trial in xrange(trials): for id in randomiseStudentKeys(True): stud_id = id student = students[id] if student.preferences: temp_alloc = SimAllocation(ident, session_id, stud_id) ranks = sorted(student.preferences.keys()) for rank in ranks: # Now we begin to try giving him/her a project proj = random.choice(list(student.preferences[rank])) if not (proj.allocated or proj.blocked or proj.own_project): student.allocated_project = proj student.allocated_proj_ref = proj.proj_id student.allocated_rank = rank allocSuccessActions(proj) temp_alloc.alloc_proj = proj.proj_id # ... we can set the allocated project details temp_alloc.alloc_proj_rank = rank session.add(temp_alloc) break ident += 1 # Increment the primary key session.add(temp_alloc) session.flush() session_id += 1 resetData() # Resets all data session.commit() ####### END CODE 1 ####### Later on I'm using this session to run some calculations on my data. For example: ####### BEGIN CODE 2 ######## sid = 4545 project_id_list = list(students[sid].preferences) for project_id in project_id_list gotcha = session.query(SimAllocation).filter(SimAllocation.student_id == sid).filter(PP.SimAllocation.alloc_proj == project_id).count() ###### END CODE 2 ####### Simply, this line counts how many times a certain student was allocated each project from his list when using the Monte-Carlo simulation from ### CODE 1 ### above. +++ Questions +++ 1. Is this the correct way to use sessions or am I sort of abusing them? 2. When should I close a session? 3. I got the following error after trying to use copy.deepcopy() on one of my dictionaries. File "Main.py", line 106, in <module> OPTM.preoptAlloc(some_best) File "XXXXXXXX/Optimisation.py", line 48, in preoptAlloc sid = projs_preopt[sim.alloc_proj_ref].proj_sup File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/ lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/ attributes.py", line 158, in __get__ return self.impl.get(instance_state(instance), instance_dict(instance)) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/ lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/ attributes.py", line 377, in get value = callable_() File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/ lib/python2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/ state.py", line 185, in __call__ attr.impl.key in unmodified 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 1864, in _load_scalar_attributes "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 Is this something to do with the way I've been using the sessions? --- Thanks, Az -- 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.