SQLAlchemy: who is in charge of the "session"? ( and how to unit-test with sessions )
- by Nick Perkins
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?