Bottle和SQLAlchemy多个数据库

时间:2012-07-17 20:30:00

标签: python sqlalchemy wsgi bottle

在下面的代码中,如果我点击/ testDbOne我没有错误,如果我点击/ testDbTWo我收到以下错误:

TypeError('testDbTwo() takes exactly 1 argument (0 given)',)

如果我交换engine1和engine2的位置,那么testDbOne中断并且testDbTwo工作。我做错了什么我不能让SQLAlchemy创建两个会话并将它们作为插件安装到瓶子中?

Base = declarative_base()

#engine1
engine_s = create_engine('mysql://user:pass@localhost/dbone', echo=True)
create_session_s = sessionmaker(bind=engine_s)
bottle.install(SQLAlchemyPlugin(engine_s, Base.metadata, keyword="dbone"))

#engine2
engine = create_engine('mysql://user:pass@localhost/dbtwo', echo=True)
create_session = sessionmaker(bind=engine)
bottle.install(SQLAlchemyPlugin(engine, Base.metadata, keyword="dbtwo"))

#create the actual database sessions
dboneSession = create_session_s()
dbTwoSession = create_session()

@route("/testDbOne")
def testUserOne(dbone):
    return "no error here"

@route("/testDbTwo")
def testDbTwo(dbtwo):
    return "no error here"

1 个答案:

答案 0 :(得分:1)

如果您将sqlalchemy信息添加到您的路线,那么它开始正常工作。例如:

@route("/testDbOne", sqlalchemy=dict(keyword='dbone'))
def testUserOne(dbone):
    return "no error here"

@route("/testDbTwo", sqlalchemy=dict(keyword='dbtwo'))
def testDbTwo(dbtwo):
    return "no error here"

似乎适合我。