I think I made some progress and wanted to share it.  I'm forced to use 
threadlocal because TurboGears uses it.  My SA definitions exist outside 
of TG so I have to associate a connection to them after they are 
imported, and that's where I've been hung up.

When sqlalchemy.mods.threadlocal is imported, a global SessionContext is 
created and associated to all mappers (from the docs) and references via 
sqlalchemy.objectstore.  That's great, but how to I hook up an engine? 
Without threadlocal, I would use create_session(bind_to=engine), but my 
session already exists as sqlalchemy.objectstore and I can't find any 
way to assign an engine to it.  A solution, I found was to define the 
engine for the metadata.  So I would fetch a class and get to it's 
metadata like this:

     metadata = class_mapper(Project).mapped_table.metadata

Then connect like so:

     metadata.connect(my_engine)

Now, every class that uses metadata is connected when using the 
threadlocal SessionContext.  So now I can to this:

     project = sqlalchemy.objectstore.query(Project).select()[0]

But I would get an error if I did this:

     print project.reviewer

Because reviewer uses a different DynamicMetaData instance.  So to get 
it connected I do:

     class_mapper(User).mapped_table.connect(my_engine)

Now I can access project.reviewer.

Now I'm off to apply this in TG.

Randall

Randall Smith wrote:
> Michael Bayer wrote:
> 
>>it seems from symptom 1 and symptom 2 that your objects are not  
>>finding their way into Sessions, or are getting removed.  if you  
>>stick with the simpler pattern above, it should be clear what Session  
>>your objects are a part of.  symptom 2 also should be raising an  
>>error instead of returning None/blank list; you should use the latest  
>>trunk until i release 0.2.4 which fixes this issue.
>>
> 
> 
> I can't use the simple pattern.  I'm using a RESTful pattern in which 
> the data record's ID is taken from the URL and looked up before the 
> requested action is taken.  So in this case, _get_item looks up the 
> project and passes it to the save method.  So how do I operate on 
> 'project' after it is received by the saved method?  I thought I could 
> retrieve the session and go from there.
> 
>      def _get_item(self, atom):
>          session = create_session(engine)
>          query = session.query(Project)
>          project = query.get(int(atom))
>          return project
> 
>      def save(self, project, **data):
>          session = sqlalchemy.object_mapper(project).get_session()
>          project.title = data['title']
>          session.flush()
>          tg.flash("Changes saved!")
>          raise tg.redirect("/")
> 
> The simple patterns never seem to suffice past the tutorial stage. 
> Requirements get complicated really quickly.
> 
> Randall
> 
> 
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to