SQLAlchemy批量删除后获取已删除的行

时间:2018-06-04 19:08:03

标签: python sqlalchemy bulk-delete

答案here现已过时。

我尝试的东西不起作用:

@event.listens_for(Session, 'after_bulk_delete')
def receive_before_delete(delete_context):
  # Using context.statement
  context = delete_context.context
  delete_context.session.execute(context.statement).fetchall()

  # Using delete_context.query
  delete_context.session.execute(delete_context.query.statement).fetchall()

  # Using delete_context.result
  result = delete_context.result
  while result.returns_rows:
      row = result.fetchone()

  # Not using delete_context.session
  # Throws: *** UnboundExecutionError: This AnnotatedSelect object is not directly bound to a Connection or Engine.Use the .execute() method of a Connection or Engine to execute this construct.
  delete_context.query.statement.execute()   

前两种方法返回空列表,而受影响的行实际上已被删除。

delete_context.result.rowcount返回将删除的正确行数。

query_context.statementNone

0 个答案:

没有答案