Re: working with multiple databases

2007-01-24 Thread Max Ischenko

Hi,

Update: looks like I more or less solved the problem.

For each of the databases I need to get access through SA I have a code
like this in models/__init__.py:

blog_meta = DynamicMetaData()

def init_blog_meta(meta):
engine = get_db_engine('blog')
meta.connect(engine)
wp_posts_tbl = Table('wp_posts', meta, autoload=True)
# ...

The get_db_engine() function (defined in lib.database module) calls
pylons.database.create_engine() with appropriate dburi. I am a bit
worried about performance (will those engine pools be re-inited for
each request?) but I suppose this is something I can fix later if
needed.

Hopefully someone will find this useful.

Max.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~--~~~~--~~--~--~---



working with multiple databases

2007-01-23 Thread Max Ischenko

Hello,

I'm struggling to setup SA/Pylons for a multidatabase env without much
luck.

As far as I understand, I need a session per database. In Pylons, I get
it for free via session_context binding. In other words, I have setup
a session_context object for each of the database I need to work with.

 db.get_db_session('blog')
sqlalchemy.orm.session.Session object at 0xb7337a8c
 db.get_db_session('cdb')
sqlalchemy.orm.session.Session object at 0xb720f7ec

Nevertheless, it is not usable:

 model.blog_wp_posts_tbl.count().execute()
Traceback (most recent call last):
  File console, line 1, in ?
  File build/bdist.linux-i686/egg/sqlalchemy/sql.py, line 474, in
execute
  File build/bdist.linux-i686/egg/sqlalchemy/sql.py, line 411, in
execute
except Exception ,e:
InvalidRequestError: This Compiled object is not bound to any engine.

I suppose my metadata setup is wrong. Currently it looks like this:


blog_meta = DynamicMetaData()

blog_wp_posts_tbl = Table('wp_posts', blog_meta,
Column('ID', Integer, primary_key=True),
Column('guid', String(255)),
Column('post_author', Integer, ForeignKey('wp_users.ID'),
nullable=False),
Column('post_content', Unicode),
)


Do I need to use separate DynamicMetaData() for each db conn? Do I need
to connect single metadata to the correct session? SA docs shows how to
connect metadata to a engine but all I have is a session.


Max.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~--~~~~--~~--~--~---