簡體   English   中英

Bottle和SQLAlchemy多個數據庫

[英]Bottle and SQLAlchemy multiple databases

在下面的代碼中,如果我命中/ 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"

如果將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"

似乎為我工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM