Search code examples
pythonsqlalchemypyramid

Multiple databases in Pyramid


Firstly, I'm new to Python.

Now that that's out of the way. I'm trying to access data on 2 different databases and display information on one view.

http://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/pylons/models.html#multiple-databases

This^ seems incomplete after the first paragraph. I have the first database working and complete, and it works great. Now I'm trying to incorporate a second database.

I can't find anywhere what my development.ini file should look like if I have a second database. I have this:

sqlalchemy.url = mssql+pyodbc://[databasestuffsredacted]

and I figured something like

db2 = mssql+pyodbc://[seconddbstuffsredacted]

(both databases use the same type and driver) From a previous stack overflow question, this was mentioned:

DBSession2 = orm.scoped_session(orm.sessionmaker(extension=ZTE())

But, how in the world does that talk to my second database. I'm missing a crucial piece here that I think should have been in the document above after the first paragraph.


Solution

  • you can write it in a module

    engine= create_engine('postgresql://user:pswd@localhost/db1')
    DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
    DBSession.configure(bind=engine)
    
    engine2= create_engine('postgresql://user:pswd@localhost/db2')
    DBSession2 = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
    DBSession2.configure(bind=engine2)