引擎的SQLAlchemy更改数据库

时间:2019-10-31 12:07:47

标签: python sqlalchemy flask-sqlalchemy

我当前的烧瓶+ SQLAlchemy应用程序存在一些问题。我创建一个数据库/引擎实例,该实例连接到SQL Server中始终存在的数据库'master',然后,创建测试数据库,并将现有引擎的数据库从'master'更改为该新数据库。我的问题是,我似乎没有为此引擎正确地对数据库进行“重定位”。

def create_db(self):
    url = copy(self.db.engine.url)  # current url points to database 'master'
    engine = create_engine(url, connect_args={'autocommit': True}, isolation_level='AUTOCOMMIT')
    res = engine.execute(f"exec dbo.create_my_test_temp_db")
    tempdbname = res.fetchone()[0]  # this is the new DB the stored proc created for us
    res.close()
    engine.dispose()
    self.db.engine.url.database = tempdbname  # repoint to the new DB (doesn't work)
    return tempdbname

我必须使用存储过程dbo.create_my_test_temp_db,它是服务器端的,因为它在创建数据库时起到了神奇的作用。我在URL中更改了数据库,引擎的连接字符串确实显示了新数据库,但是所有操作都针对master执行。

如何将现有引擎的数据库更改为另一个数据库?

0 个答案:

没有答案