SQLAlchemy:不能使event.listens.for()工作

时间:2016-12-04 10:42:06

标签: python mysql sqlalchemy

我是SQLAlchemy的新手,并因其易用性和事件机制而加入其中。我希望在插入一些数据后执行一个方法,但是我一直遇到类型"属性[...]"的几个错误。与我所指的对象有关

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, ForeignKey
from sqlalchemy import Integer, UnicodeText
from sqlalchemy import event

#variables feching is hidden
db = create_engine("mysql://"+dbLogin+":"+dbPassword+"@"+dbAddress+"/"+dbDatabase)
metadata = MetaData(db)
#the log table is not explicitly defined but rather uses the autoload function
logtable = Table('logs', metadata, autoload=True)

#and here I am trying to start "processLogChanged" after_insert on the "logs" table
@event.listens_for(logtable, "after_insert")
def processLogChanged():
    print "---- the log table has changed"

我显然做错了什么,但我一直在搜索很多问题/答案,而且在这里找不到,如果你能帮我一把,那就太棒了

例如,在这种情况下,这是堆栈

    Traceback (most recent call last):
     File "optra.py", line 144, in <module>@event.listens_for(logtable, "after_insert")
    File "/home/jpp/anaconda2/lib/python2.7/site-packages/sqlalchemy/event/api.py", line 124, in decorate
    listen(target, identifier, fn, *args, **kw)
  File "/home/jpp/anaconda2/lib/python2.7/site-packages/sqlalchemy/event/api.py", line 89, in listen
    _event_key(target, identifier, fn).listen(*args, **kw)
  File "/home/jpp/anaconda2/lib/python2.7/site-packages/sqlalchemy/event/registry.py", line 194, in listen
    dispatch_collection = getattr(target.dispatch, identifier)
  File "/home/jpp/anaconda2/lib/python2.7/site-packages/sqlalchemy/event/base.py", line 95, in __getattr__
    raise AttributeError(name)
AttributeError: after_insert

谢谢

0 个答案:

没有答案