TG and Sqlalchemy database binding

0 views
Skip to first unread message

percious

unread,
Nov 23, 2007, 12:02:41 AM11/23/07
to TurboGears Trunk
My dbmechanic object takes in the metadata from the model as well as a
link to the sessionmaker class. Something like this:

from sprockets.lib.base import BaseController
from tg import expose, validate
from sprockets.model import *
from pylons.controllers.util import redirect_to
session = Session()

from dbsprockets.dbmechanic.dbmechanic import DBMechanic
from dbsprockets.saprovider import SAProvider

class RootController(BaseController):
dbmechanic = DBMechanic(SAProvider(metadata), Session)

@expose('sprockets.templates.index')
def index(self):
session.save(User('wendy', 'Wendy Williams', 'foobar'))
session.flush()
rows=session.query(User).select()
print rows
from datetime import datetime
return dict(now=datetime.now())

The problem I am having is that when dbmechanic goes to pull the
record out of the database, the metadata is not properly bound, and
therefore dbsprockets thinks there is no table in the database:

if len(l) >1:
return
table.select().where(and_(*l)).execute().fetchall()
return table.select().execute().fetchall()>> return
table.select().execute().fetchall()
Module sqlalchemy.sql.expression:970 in execute
<< e = self.bind
if e is None:
raise exceptions.InvalidRequestError("This Compiled
object is not bound to any Engine or Connection.")
return e._execute_clauseelement(self, multiparams,
params)>> raise exceptions.InvalidRequestError("This Compiled object
is not bound to any Engine or Connection.")
sqlalchemy.exceptions.InvalidRequestError: This Compiled object is not
bound to any Engine or Connection.

My question is this:
Where/when does the metadata get bound to the database connection, and
how can I ensure that my Controller has access to that bound metadata
when the provider is created.

Thanks,
chris

Mark Ramm

unread,
Nov 23, 2007, 11:40:41 AM11/23/07
to turbogea...@googlegroups.com
Yea, this might be cause by a bug we fixed in database session
configuraiton at the last sprint.

The changeset for that fix is:

http://trac.turbogears.org/changeset/3652

But basically the database configuration is in config/environment.py
and we need to call the confgure method that sets everything up for
us, rather than just setting bind on the DBSession class manually.


--Mark

--
Mark Ramm-Christensen
email: mark at compoundthinking dot com
blog: www.compoundthinking.com/blog

percious

unread,
Nov 23, 2007, 11:20:29 PM11/23/07
to TurboGears Trunk
Yeah, that didn't work. I am using revision 3572. (latest svn up)
The only thing that changed was the development.ini template.
-chris

percious

unread,
Nov 24, 2007, 12:06:42 AM11/24/07
to TurboGears Trunk
I noticed in websetup.py there is a line of code that reads like the
following:

model.metadata.create_all(bind=config['pylons.g'].sa_engine)

since the metadata us un-bound, I think a similar call would have to
be made before my root controller is initialized, OR
dbmechanic would have to search for the appropriate SA binding
whenever a request is made.

I tried to put something similar in the code, but I the config object
is not readily loaded in the root.py. (I tried to hack around the
problem)

Any ideas on which direction I can take here. A hacky but temporary
solution would get me moving again? The latest code is up on the
dbsprockets repository. Feel free to d/l it.

cheers.
-chris

Mark Ramm

unread,
Nov 24, 2007, 10:55:35 PM11/24/07
to Christopher Perkins, turbogea...@googlegroups.com
The attached test project seems to work with dbsprockets for me.

This is slightly adapted from your sample code in another thread.
And was run against a very recient checkout of DBSprockets.

This is good stuff, thanks!

--Mark

On Nov 24, 2007 7:06 PM, Mark Ramm <mark.mch...@gmail.com> wrote:
> Session is kinda magical. You don't need to instantiate the Session
> class, you can just classmethods which transparently proxy to a
> threadlocal instance (or if no threadlocal instance exists, create
> one). So, drop session = Session()
>
> I'm also wondering what you've got since we replaced Session with
> DBSession in the model...
>
> I've downloaded the dbsprockets code, and I'm willing to hack on it a
> bit to see if I can't help get this worked out, but it would be
> helpful to know how to run it.


>
> --Mark
>
>
> On Nov 23, 2007 12:02 AM, percious <ch...@percious.com> wrote:
> >

Mark Ramm

unread,
Nov 24, 2007, 10:56:26 PM11/24/07
to Christopher Perkins, turbogea...@googlegroups.com
Oops, looks like I didn't get that attachment attached.
dbstest.zip
Reply all
Reply to author
Forward
0 new messages