I was able to run both ming and sqlalchemy by adding the following to 
project.config.app_cfg and project.model.__init__:

*app_cfg.py*

#added to imports
> from tg.configuration.utils import coerce_config
> from tg.configuration import AppConfig, config
> from project.model import init_model
> from paste.deploy.converters import asbool, asint
>
>
> # add this before base_config =
> class MultiDBAppConfig(AppConfig):
>
>     def setup_ming(self):
>         """Setup MongoDB database engine using Ming"""
>         try:
>             from ming import create_datastore
>             def create_ming_datastore(url, database, **kw):
>                 if database and url[-1] != '/':
>                     url += '/'
>                 ming_url = url + database
>                 return create_datastore(ming_url, **kw)
>         except ImportError: #pragma: no cover
>             from ming.datastore import DataStore
>             def create_ming_datastore(url, database, **kw):
>                 return DataStore(url, database=database, **kw)
>
>         def mongo_read_pref(value):
>             from pymongo.read_preferences import ReadPreference
>             return getattr(ReadPreference, value)
>
>         datastore_options = coerce_config(config, 'ming.connection.', 
> {'max_pool_size':asint,
>                                                                        
> 'network_timeout':asint,
>                                                                        
> 'tz_aware':asbool,
>                                                                        
> 'safe':asbool,
>                                                                        
> 'journal':asbool,
>                                                                        
> 'wtimeout':asint,
>                                                                        
> 'fsync':asbool,
>                                                                        
> 'ssl':asbool,
>                                                                        
> 'read_preference':mongo_read_pref})
>         datastore_options.pop('host', None)
>         datastore_options.pop('port', None)
>
>         datastore = create_ming_datastore(config['ming.url'], 
> config.get('ming.db', ''), **datastore_options)
>         config['pylons.app_globals'].ming_datastore = datastore
>
>         '''Setup SQLAlchemy database engine(s)'''
>         from sqlalchemy import engine_from_config
>         engine = engine_from_config(config, 'sqlalchemy.')
>         # engine1 should be assigned to sa_engine as well as your first 
> engine's name
>         config['tg.app_globals'].sa_engine = engine
>         # Pass the engines to init_model, to be able to introspect tables
>         init_model(datastore, engine)
>
> #base_config = AppConfig()
> base_config = MultiDBAppConfig()
>

*then in __init__.py*

#ming imports
> import ming.orm
> from session import mainsession, DBSession
> #sqlalchemy  imports
> from zope.sqlalchemy import ZopeTransactionExtension
> from sqlalchemy.orm import scoped_session, sessionmaker
> from sqlalchemy import MetaData
> from sqlalchemy.ext.declarative import declarative_base
>
  

> #ming

 

> maker = sessionmaker(autoflush=True, autocommit=False,
>                      extension=ZopeTransactionExtension())
>
 

> #sqlalchemy 

 

> DBSession2 = scoped_session(maker)
> DeclarativeBase2 = declarative_base()
> DeclarativeBase2.metadata = MetaData()
>
>  

> def init_model(datastore, engine):
>     """Call me before using any of the tables or classes in the model.""" 

    #ming

 

>     mainsession.bind = datastore
>     ming.orm.Mapper.compile_all()
>
>     for mapper in ming.orm.Mapper.all_mappers():
>         mainsession.ensure_indexes(mapper.collection)
>
 

>     #sqlalchemy 
>     metadata2 = DeclarativeBase2.metadata
>     DBSession2.configure(bind=engine)
>     metadata2.bind = engine
>
> # Import your model modules here.
> from project.model.auth import User, Group, Permission
>

Hope this saves someone else some time.

-- 
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/turbogears.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to