# sqlalchemy.exc.StaleDataError: UPDATE statement on table 'users' expected to update 1 row(s); 0 were matched.

- **ID:** `python/sqlalchemy-stale-data-race`
- **Domain:** python
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Another transaction deleted or modified the row between the SELECT and UPDATE, causing the ORM's version check or rowcount to fail.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.4.x | active | — | — |
| 2.0.x | active | — | — |

## Workarounds

1. **** (90% success)
   ```
   Implement optimistic locking with version_id_col: class User(Base): __mapper_args__ = {'version_id_col': version}
   ```
2. **** (85% success)
   ```
   Catch StaleDataError and retry the whole transaction with fresh data: for attempt in range(3): try: update() break except StaleDataError: session.rollback()
   ```

## Dead Ends

- **** — Autoflush doesn't affect the underlying row version; the issue is concurrent modification, not flush timing. (70% fail)
- **** — Refresh may still see stale data if the other transaction hasn't committed yet; it doesn't solve the race condition. (60% fail)
