On 04/03/2017 09:53 AM, Артём Мигда wrote:
Hi,

I trying to use schema_translate_map in my project and I encountered
with strange behavior of execution_options binding. Here's a code with
session creation:

DBSession = scoped_session(sessionmaker())
session = DBSession()
session.connection(execution_options={
    'schema_translate_map`: {None: 'local'}
})
session.connection()._execution_options  # here's our execution options
session.commit()
session.connection()._execution_options  # here's empty dict

So, my question is can we bind execution_options to session (maybe
through hooks) not to connection for using 'schema_translate_map' after
commit()?

this is a good point. It might be nice if execution_options themselves could be part of Session() to be automatically used by the Session when it makes a new connection.

You can get this now by passing a bind up front:

session = DBSession(bind=engine.execution_options(schema_translate_map={None: "local"})

that is, do it when you construct the Session with the engine. This does however require that you have a hold of that engine.

Another approach might be more like the "hook" you describe, to set up Session.connection() when the session's "transaction" starts up using the after_transaction_create, while this is possible, it unfortunately requires some esoteric steps to work correctly because the SessionTransaction is not yet assigned to the Session:


@event.listens_for(DBSession, "after_transaction_create")
def _setup_options(session, transaction):
    transaction.connection(session.get_bind(None), execution_options=...)

or

@event.listens_for(DBSession, "after_transaction_create")
def _setup_options(session, transaction):
    session.transaction = transaction
    session.connection(execution_options=...)


if you have the Engine up front, just constructing the Session with the options would be best for now. Also I would approve of adding execution_options as a constructor argument to Session.





--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and
Verifiable Example. See http://stackoverflow.com/help/mcve for a full
description.
---
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[email protected]>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to