I'm trying to connect my turbogears application to 2 db
1. sqlite for all the turbogears stuff (users,groups, etc)
2. mssql - to write to a certain table (but I can't use it for all of
the turbogears tables, other models etc - very specific write access)

I followed the tutorial in the docs and also those:
http://blog.curiasolutions.com/2009/07/multiple-databases-in-turbogears-2/
http://www.blog.pythonlibrary.org/2009/06/13/using-multiple-databases-in-turbogears-2/
I commented the original sqlalchemy.url in development.ini and wrote
the sqlalchemy.first.url and sqlalchemy.second.url
I edited the app_cfg like this:
from tg.configuration import AppConfig,config
from pylons import config as pylons_config
from priorityagent.model import init_model

import priorityagent
from priorityagent import model
from priorityagent.lib import app_globals, helpers
class MultiDBAppConfig(AppConfig):
    def setup_sqlalchemy(self):
        '''Setup SQLAlchemy database engine(s)'''
        from sqlalchemy import engine_from_config
        engine1 = engine_from_config(pylons_config,
'sqlalchemy.first.')
        engine2 = engine_from_config(pylons_config,
'sqlalchemy.second.')
        # engine1 should be assigned to sa_engine as well as your
first engine's name
        config['pylons.app_globals'].sa_engine = engine1# different
tutorials state different things about commenting or uncommenting this
line out
        config['pylons.app_globals'].sa_engine_first = engine1
        config['pylons.app_globals'].sa_engine_second = engine2
        # Pass the engines to init_model, to be able to introspect
tables
        init_model(engine1, engine2)

#base_config = AppConfig()
base_config = MultiDBAppConfig()
I added to model.__init__ as follows:

maker2 = sessionmaker(autoflush=True, autocommit=False,
                     extension=ZopeTransactionExtension())
DBSession2 = scoped_session(maker2)

DeclarativeBase2 = declarative_base()

metadata2 = DeclarativeBase2.metadata
and in the init_model:
  DBSession.configure(bind=engine1)
  DBSession2.configure(bind=engine2)

    metadata.bind = engine1
    metadata2.bind = engine2

 I wrote the class that supposed to use the second db (mssql) as
followes:
# -*- coding: utf-8 -*-
"""Sample model module."""

from sqlalchemy import *
from sqlalchemy.orm import mapper, relation
from sqlalchemy import Table, ForeignKey, Column
from sqlalchemy.types import Integer, Unicode, Boolean,String
#from pylons import config as pylons_config # added this while trying
to figure this out

#from sqlalchemy.orm import relation, backref

from priorityagent.model import DeclarativeBase2, DBSession2 # added
DBSession2 while trying to figure this out- it isn't in the tutorial
#from priorityagent.model import init_model,metadata2 # added this
while trying to figure this out
#engine2 = engine_from_config(pylons_config, 'sqlalchemy.second.') #
added this while trying to figure this out
#
#metadata2.bind = engine2 # added this while trying to figure this out


    class Activities(DeclarativeBase2):
       __tablename__ = 'specific tablename''
       __table_args__=  {'autoload':True
                         #,'autoload_with':engine2 # added this while
trying to figure this out - didn't need this when I had a standalone
sqlalchemy class doing autoload
                         }

but something doesn't seem to work right -
when running paster setup-app or paster shell I get:

   sqlalchemy.exc.UnboundExecutionError: No engine is bound to this
Table's MetaDat
   a. Pass an engine to the Table via autoload_with=<someengine>, or
associate the
   MetaData with an engine via metadata.bind=<someengine>

when I add the 'autload_with':engine2 I get an error message  It
doesn't know engine2

when I added:
    from pylons import config as pylons_config
    engine2 = engine_from_config(pylons_config, 'sqlalchemy.second.')
I get:
     File "D:\Python27\learn\agent\agent\lib\site-packages
\sqlalchemy-0.7.3-py2.7.egg\sqlalchemy\engine\__init__.py", line 297,
in engine_from_config
   url = opts.pop('url')
   KeyError: 'url'

I thought maybe editing schema.py with the sa_engine would be needed
so I added there:
 
model.metadata.create_all(bind=config['pylons.app_globals'].sa_engine_first)
 
model.metadata2.create_all(bind=config['pylons.app_globals'].sa_engine_second)

but it didn't make and difference

you can follow my different shots at this problem in the comments but
I'm practically in the dark here with turbogears internals and very
vague docs..

I suspect this is connected to not having an __init__ method so that
model.__init__ maybe isn't called? but I don't really need one here -
I'm using autoloading on a preexisting table

when I used a similiar  class directly with sqlalchemy without
turbogears2, (add the engine with the connection url, import the
session , etc) it does work but that doesn't help me too much, since I
want this class to be a part of a turbogears application.

I'll be glad for any help/hint about this

-- 
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