我试图用来schema_translate_map
更改架构:
Base = declarative_base()
class DataAccessLayer():
def __init__(self):
conn_string = "mysql+mysqlconnector://root:root@localhost/"
self.engine = create_engine(conn_string)
Session = sessionmaker()
Session.configure(bind=self.engine)
self.session = Session()
def change_schema(self):
self.session.connection(execution_options={"schema_translate_map": {"belgarath": "belgarath_test"}})
class Player(Base):
__tablename__ = "player"
__table_args__ = {'schema': "belgarath"}
id_ = Column(Integer, primary_key=True)
dal = DataAccessLayer()
dal.change_schema()
qry = dal.session.query(Player.id_)
print(qry)
但是,SQL显示为:
SELECT belgarath.player.id_ AS belgarath_player_id_
FROM belgarath.player
代替:
SELECT belgarath_test.player.id_ AS belgarath_test_player_id_
FROM belgarath_test.player
我要去哪里错了?
如果您只是追加.all()
到您的qry
:
from sqlalchemy import Integer
from sqlalchemy import Column
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class DataAccessLayer():
def __init__(self):
conn_string = "sqlite:///:memory:"
#conn_string = "mysql+mysqlconnector://root:root@localhost/"
self.engine = create_engine(conn_string)
Session = sessionmaker()
Session.configure(bind=self.engine)
self.session = Session()
def change_schema(self):
self.session.connection(execution_options={"schema_translate_map": {"belgarath": "belgarath_test"}})
class Player(Base):
__tablename__ = "player"
__table_args__ = {'schema': "belgarath"}
id_ = Column(Integer, primary_key=True)
dal = DataAccessLayer()
dal.change_schema()
qry = dal.session.query(Player.id_)
print(qry.all())
输出(无跟踪):
OperationalError: (sqlite3.OperationalError) no such table: belgarath_test.player
[SQL: SELECT belgarath_test.player.id_ AS belgarath_player_id_
FROM belgarath_test.player]
(Background on this error at: http://sqlalche.me/e/13/e3q8)
我不是专家,但是我想这可能与以下问题有关:
模式转换功能发生在编译器内部,这是完全错误的。模式分配应该在生成SQL之后进行,因此我们只需要一个缓存键。这是按照#5002的思路进行的,但是我认为,即使是与AKED等一起使用的现有缓存键机制,也都需要将架构转换完全从编译器中拉出1.4版,并将其添加到从ExecutionContext生成的转换中,扩展逻辑参数集。模式转换旨在为数百/数千个模式提供服务,因此发生这种情况时,必须更改预缓存。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句