为什么 schema_translate_map 不更改架构?

Why doesn't schema_translate_map change schema?

我正在尝试使用 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

我哪里错了?

我猜查询 API 不知道 execution_options 的连接。尽量不要混用这两种方法。

dal = DataAccessLayer()
dal.change_schema()
qry = dal.session.connection().execute(Player.__table__.select())
print(qry)

结果:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: belgarath_test.player
[SQL: SELECT belgarath_test.player.id_ 
FROM belgarath_test.player]
(Background on this error at: http://sqlalche.me/e/13/e3q8)

尝试将 .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)

我不是专家,但我猜这可能与以下内容有关issue

the schema translate feature takes place within the compiler and this is plainly wrong. the schema assignment should be taking place after the SQL is generated so that we only need one cache key. This is along the lines of #5002 however I think even the existing cache key mechanism used with baked etc. needs to pull the schema translate out of the compiler entirely for 1.4 and add it to the translations which occur from the ExecutionContext, along with the expanding parameter sets of logic. schema translate is intended to service many hundreds / thousands of schemas so having this occur pre-cache has to change.