简体   繁体   English

SQLAlchemy(无ORM):更新语句未提交

[英]SQLAlchemy (no ORM): update statement not committing

I have this (anonymized) function in my database module: 我的数据库模块中有此(匿名)功能:

def fix_publishing_dates(row_id, last_time=None, next_time=None,
    next_index=1, user="python_script"):
  sql = """
  UPDATE
    schema.table
  SET
    last_time = :last_time
  , next_time = :next_time
  , next_index = :next_index
  , col4 = SYSDATE
  , col5 = :user_id
  , is_active = 1
  WHERE
    id = :row_id
  """
  with closing(Session()) as s:
    with s.begin_nested():
      user_id = get_userid_by_name(user)
      args = dict(
          last_time=last_time,
          next_time=next_time,
          next_index=next_index,
          row_id=row_id,
          user_id=user_id,
          )
      s.execute(sql, args)
      s.flush()
    s.commit()

For some reason, this is not working. 由于某种原因,这是行不通的。 I query the above table for is_active=1, and I get zero rows. 我在上表中查询is_active = 1,得到零行。 Am I doing something obviously wrong here? 我在这里做错什么吗?

Note 注意

I don't want to use SQLAlchemy ORM and add lots of boilerplate Table classes* for this; 我不想使用SQLAlchemy ORM为此添加很多样板表类*; I just like using Session() with text queries for the transaction support. 我就像将Session()与文本查询一起用于事务支持。

*: nor slow down my startup time with introspection; *:也不会因为自省而减慢我的启动时间; the network pipe to this database is slow. 到此数据库的网络管道很慢。

Edit 1 编辑1

  • I'm using an Oracle 11 database through cx_oracle. 我正在通过cx_oracle使用Oracle 11数据库。
  • In case it matters, one of the bound values is sometimes None/ 如果很重要,则绑定值之一有时为None /
  • This code (anonymized differently) also isn't working: 此代码(以不同的方式进行匿名化)也无法正常工作:

     def fix_publishing_dates(**kwargs): sql = insert_query_here user_id = get_userid_by_name(user) args = dict(kwargs) print "*" * 50 print "* About to update database with values: {}".format(args) print "*" * 50 result = engine.execute(sql, args) print "Row count is:", result.rowcount #import ipdb;ipdb.set_trace() #s.commit() 

I don't think the Sessions will manage transactions without using ORM objects. 我认为在不使用ORM对象的情况下,Sessions不会管理事务。 But you can just use the transactions on the engine directly: 但是您可以直接在引擎上使用事务:

with engine.begin() as conn:
    conn.execute(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM