SQLAlchemy: who is in charge of the "session"? ( and how to unit-test with sessions )

Posted by Nick Perkins on Stack Overflow See other posts from Stack Overflow or by Nick Perkins
Published on 2010-04-09T23:06:04Z Indexed on 2010/04/09 23:13 UTC
Read the original article Hit count: 276

Filed under:
|
|
|
|

I need some guidance on how to use session objects with SQLAlchemy, and how to organize Unit Tests of my mapped objects.

What I would like to able to do is something like this:

thing = BigThing() # mapped object
child = thing.new_child() # create and return a related object
thing.save() # will also save the child object

In order to achieve this, I was thinking of having the BigThing actually add itself ( and it's children ) to the database -- but maybe this not a good idea?

One reason to add objects as soon as possible is Automatic id values that are assigned by the database -- the sooner they are available, the fewer problems there are ( right? )

What is the best way to manage session objects? Who is in charge of the session?
Should it be created only when required? or saved for a long time?

What about Unit Tests for my mapped objects?...how should the session be handled? Is it ever OK to have mapped objects just automatically add themselves to a database? or is that going to lead to trouble?

© Stack Overflow or respective owner

Related posts about python

Related posts about sqlalchemy