The UPDATE statement in table xxx 'is expected to update 1 row; 0 have been mapped to Zope transactionmanager

I am running the transaction manager Pyramid + Zope + SQLAlchemy + PostgreSQL. In some cases, I saw an error StaleDataErrorin the Pyramid web application, which should have a very trivial idea of ​​updating a single row in the database. Since the error occurs outside the normal view boundary and is not repeated, it is quite difficult to debug.

I guess this may have something to do with broken database connections or transaction life cycle. However, I do not know how to start debugging the system, so I ask what can cause this, and, moreover, how to fix such errors.

UPDATE statement on table 'xxx' expected to update 1 row(s); 0 were matched.

Stacktrace (most recent call last):

  File "pyramid/tweens.py", line 20, in excview_tween
    response = handler(request)
  File "pyramid_tm/__init__.py", line 94, in tm_tween
    reraise(*exc_info)
  File "pyramid_tm/compat.py", line 15, in reraise
    raise value
  File "pyramid_tm/__init__.py", line 82, in tm_tween
    manager.commit()
  File "transaction/_manager.py", line 111, in commit
    return self.get().commit()
  File "transaction/_transaction.py", line 280, in commit
    reraise(t, v, tb)
  File "transaction/_compat.py", line 55, in reraise
    raise value
  File "transaction/_transaction.py", line 271, in commit
    self._commitResources()
  File "transaction/_transaction.py", line 417, in _commitResources
    reraise(t, v, tb)
  File "transaction/_compat.py", line 55, in reraise
    raise value
  File "transaction/_transaction.py", line 389, in _commitResources
    rm.tpc_begin(self)
  File "/srv/pyramid/trees/venv/lib/python3.4/site-packages/zope/sqlalchemy/datamanager.py", line 90, in tpc_begin
    self.session.flush()
  File "sqlalchemy/orm/session.py", line 2004, in flush
    self._flush(objects)
  File "sqlalchemy/orm/session.py", line 2122, in _flush
    transaction.rollback(_capture_exception=True)
  File "sqlalchemy/util/langhelpers.py", line 60, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "sqlalchemy/util/compat.py", line 182, in reraise
    raise value
  File "sqlalchemy/orm/session.py", line 2086, in _flush
    flush_context.execute()
  File "sqlalchemy/orm/unitofwork.py", line 373, in execute
    rec.execute(self)
  File "sqlalchemy/orm/unitofwork.py", line 532, in execute
    uow
  File "sqlalchemy/orm/persistence.py", line 170, in save_obj
    mapper, table, update)
  File "sqlalchemy/orm/persistence.py", line 692, in _emit_update_statements
    (table.description, len(records), rows))
+4
2

:

TL, DR: "first()", ,

db.session.query(xxx).filter_by(field=value).first()

, . , = . , .

- SQL, .

, "first()"

BTW, SQL- ( ...)

import logging

logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
0

All Articles