looking for some solutions? You are welcome.

SOLVED: sqlalchemy class_instrument event doesn't fire


I can't figure out how to get the class_instrument event to fire. Maybe that's expected, I can't tell for sure from the docs if instrument events are only suppose to fire for custom instrumentation setups.

But the attribute_instrument does, so i figured i'd ask.

Docs here https://docs.sqlalchemy.org/en/latest/orm/events.html#module-sqlalchemy.orm.instrumentation.

from sqlalchemy import *
from sqlalchemy.event import listens_for
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

if __name__ == "__main__":
  class A(Base):
    __tablename__ = 'a'

    id = Column(Integer, primary_key=True)
    data = Column(String)

  @listens_for(A, 'class_instrument')
  def on_class_instrument(mapper, cls):
    cls.data.info['cls'] = True

  @listens_for(A, 'attribute_instrument')
  def on_attribute_instrument(cls, key, inst):
    print('attribute_instrument', cls)
    cls.data.info['attr'] = True

  a = A()
  assert A.data.info['attr']
  # Succeeds above, but fails here
  assert A.data.info['cls']

Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots

No comments: