On Nov 1, 2010, at 11:26 AM, Daniel Meier wrote:

> Well thank you guys for your help.
> 
> I found the way how Django (if you know it) separates the modules
> great. But I can also live with a single module containing all models.
> 
> I played some time with sqlalchemy now, and I created two classes in
> my db.py file (see http://pastie.org/1264141). In the User class, I
> added two functions "create" and "delete". Is it "best practice" to
> add such functions inside the respective classes?

I can't speak for the patterns that Django encourages, but IMHO its really not 
an ideal practice for general purpose usage, specifically the creation of 
Session objects inside of methods and additionally using many tiny 
transactions, one for each individual operation.

The Session represents a series of operations performed with model objects, and 
its scope is best established outside individual model methods, and instead at 
the appropriate point in your application that denotes a logical "unit of 
work".   For a web application, this means the Session is created at the start 
of the request, committed at the end of a request if changes are pending, then 
torn down at the end.  This startup/teardown should occur in exactly one place 
in the code since otherwise it becomes boilerplate.  In Pylons for example, 
this demarcation is established within the base controller class.     
Everything that your web request does should generally be in one transaction, 
occasionally two for some specific situations.   Model methods should not be 
demarcating many "micro" transactions.    Some more detail on this is at 
http://www.sqlalchemy.org/docs/orm/session.html#lifespan-of-a-contextual-session
 .

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to