# sqlalchemy.orm.exc.StaleDataError: UPDATE statement on table 'users' expected to match 1 row(s); 0 found

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

## Root Cause

An optimistic concurrency check failed because the row was modified or deleted by another transaction since it was read.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.x | active | — | — |

## Workarounds

1. **Re-query the object before updating** (95% success)
   ```
   user = session.query(User).get(user_id)
if user:
    user.name = 'New Name'
    session.commit()
   ```
2. **Use session.refresh() to reload the object** (90% success)
   ```
   session.refresh(user)
user.name = 'New Name'
session.commit()
   ```

## Dead Ends

- **Ignoring the error and retrying the update** — The row may have been deleted, so the update will still fail. (60% fail)
- **Disabling versioning in the model** — This removes concurrency protection, leading to potential data corruption. (80% fail)
