I want to execute raw sql on separate databases through sqlalchemy sessions. My session/engine is configured as follows:
db1Base = declarative_base()
db2Base = declarative_base()
DB_ENGINES['db1'] = create_engine(db1_postgres_url, **connection_args)
DB_ENGINES['db2'] = create_engine(db2_postgres_url, **connection_args)
session_factory = sessionmaker(autocommit=False, autoflush=False, twophase=True)
session_factory.configure(binds={db1Base: DB_ENGINES['db1'], db2Base: DB_ENGINES['db2']})
Session = scoped_session(session_factory)
When I do session.query(Model)
, sqlalchemy is able to figure out which engine to use depending on which base the model inherits from. But if I instead use session.execute(text(query), params)
i'll get the error UnboundExecutionError: Could not locate a bind configured on SQL expression or this Session.
Because sqlalchemy cannot figure out which engine to use. Is there a way to explicitly or implicitly specify which engine to use?
source https://stackoverflow.com/questions/77673018/session-execute-with-multiple-databases
Comments
Post a Comment