我有以下内容:
@periodic_task(run_every=crontab(minute="*/1"))
def PeriodicUpdateAgentLastRica():
query = """update agents
inner join
(select sims.consignment_agents_id as agents_id,
MAX(sims.rica_current_stamp)
as last_rica_stamp
from sims
where sims.rica_current_stamp > DATE_SUB(now(), INTERVAL 3 MONTH) and sims.consignment_agents_id is not NULL
group by sims.consignment_agents_id) as dt on dt.agents_id = agents.id
set agents.last_rica_stamp = dt.last_rica_stamp"""
res = Session.execute(query)
print res
if res.rowcount:
log.info("Updated %s agents" % res.rowcount)
#transaction.commit()
当我运行celery worker -B
这是返回的内容:
[2014-02-12 09:15:00,012: INFO/MainProcess] Received task: prepaid.models.Prepaid.PeriodicUpdateAgentLastRica[9ca091c8-595f-4163-8ddf-2742e573b90c]
[2014-02-12 09:15:01,812: WARNING/Worker-7] <sqlalchemy.engine.result.ResultProxy object at 0x776f310>
[2014-02-12 09:15:01,813: INFO/Worker-7] Updated 2923 agents
[2014-02-12 09:15:01,816: INFO/MainProcess] Task prepaid.models.Prepaid.PeriodicUpdateAgentLastRica[9ca091c8-595f-4163-8ddf-2742e573b90c] succeeded in 1.798980095s: None
即使它说它已更新:Updated 2923 agents
,当我检查数据库时,没有记录被更改。
查询没有任何问题,因为当我在mySQL Workbench中运行它时,它可以工作。
当我尝试强制AutoCommit,即
时也会发生同样的情况res = Session.execute(text(query).execution_options(autocommit=True))
所以提交,但Update
仍然不影响数据库。
为什么它不影响数据库?为什么不起作用?
更新
我也尝试过:
with transaction.manager:
#Rest of code
但它仍然没有改变数据库中的任何内容
答案 0 :(得分:5)
这看起来就像你在金字塔中使用zodb事务管理器。
您必须将会话标记为已更改
您应该执行类似
的操作from zope.sqlalchemy import mark_changed
mark_changed(Session)
如果不起作用,请尝试
from zope.sqlalchemy import ZopeTransactionExtension
Session.configure(extension=ZopeTransactionExtension('changed'))