Am 01.03.2011 um 21:52 schrieb demitri:

Hello,

I have a question about integrating exisiting SQLAlchemy code with a
TurboGears v2.1 project. I'm sure I'm doing it wrong as our program is
relatively unstable.

I have standalone code that describes a database connection, model
classes, etc. We have a fair amount of code that imports it that is
not TurboGears. I want to integrate these classes and database
connection into a new TurboGears project. These are the steps I've
taken; can someone point out where I'm going wrong or missing?

* I am not using the built-in "authentication and authorization", but
I think I said "yes" to it to have it include the database code. I
don't know if that's right.

* project/config/app_cfg.py
The quickstart included a lot of code I'm not using. I commented out
everything from "base_config.DBSession = apo_plates.model.DBSession"
to the end of the file.

* project/model/__init__.py
I understand that this should be the single place where the database
configuration lives. I also noted that TG really wants the scoped
session object to be called "DBSession", which is fine. In this class
I put:

from mymodel.DatabaseConnection import db # defines connection, scoped
session
DBSession = db.Session

Everything to "def init_model" is commented out since I define the
engine and metadata in my class. But then I don't see what I'd put in
init_model. Is this called automatically by the framework? I put a
"pass" there.

For the last line:
from mymodel.ModelClasses import * # all db table/classes defined here

* project/controllers/__init__.py
My model classes don't seem to be in scope in the controllers, so I
added this:

from mymodel.ModelClasses import *

* project/controllers/root.py
I added the following:

from project.model import DBSession #, metadata
from project import model

I don't know if the above is the correct recipe, but here is my
remaining problem: where is the session object created? In the wiki
example, I simply see:

x = DBSession.query(...

but as far as I can tell, DBSession is the *class* returned by
scoped_session, not an instance. To make things work, I do:

A scoped session is a factory for the actual sesion - but you can use it directly, for that thread, there will be a local instance. So it's perfectly fine to call DBSession.<whatever>, it should work.


session = DBSession()

on each page, but I don't think that's right. Finally, I also have to
call session.flush(), but I also don't see this in example code. We
have previously been putting the above line into project/controllers/
__init__.py, but I've been told this is Bad.

You do not need to call flush, unless you want to execute gathere SQL to e.g. create IDs. The final commit will automatically flush.

Diez

--
You received this message because you are subscribed to the Google Groups 
"TurboGears" 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/turbogears?hl=en.

Reply via email to