Hi Derrick,
I was using SQLAlchemy session events to run some code on the "before_commit" event. For example:
def pre_commit(session):
print("Session pre_commit() event")
engine = create_engine(db_uri)
session = scoped_session(sessionmaker(autocommit=False,autoflush=False,bind=engine))
event.listen(session, "before_commit", pre_commit)
If you try and use an SqlClient instance as the first argument to even.listen(), you get:
File ".../lib/python3.6/site-packages/sqlalchemy/event/api.py", line 28, in _event_key
(identifier, target))
sqlalchemy.exc.InvalidRequestError: No such event 'before_commit' for target '<class 'sqlservice.client.SQLClient'>'
I managed to get around this by using the _Session. Working code follows:
def pre_commit(session):
print("Session pre_commit() event")
class MyClient(SQLClient):
def __init__(self, echo=False):
self.config = get_default_config()
super().__init__(self.config, model_class=Base)
event.listen(self._Session, "before_commit", pre_commit)
It took a little time to figure out the above because your docs are specific to ORM events. I can see a couple of options to help others in the future but wanted to get your thoughts before taking it further:
- Update docs/event.rst to show how it can be done (see below)
- Wrap sqlservice.event (probably in src/sqlservice/event.py and handle special cases like this directly
Hi Derrick,
I was using SQLAlchemy session events to run some code on the "before_commit" event. For example:
If you try and use an SqlClient instance as the first argument to even.listen(), you get:
I managed to get around this by using the _Session. Working code follows:
It took a little time to figure out the above because your docs are specific to ORM events. I can see a couple of options to help others in the future but wanted to get your thoughts before taking it further: