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

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

## Root Cause

Optimistic concurrency control: row was modified or deleted by another transaction.

## Version Compatibility

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

## Workarounds

1. **Refresh object before update.** (90% success)
   ```
   session.refresh(user)
user.name = 'new_name'
session.commit()
   ```
2. **Use version counter in model.** (85% success)
   ```
   class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    version_id = Column(Integer, nullable=False)
    __mapper_args__ = {'version_id_col': version_id}
   ```

## Dead Ends

- **Ignoring the error and retrying without refresh.** — Stale object leads to further inconsistencies. (80% fail)
- **Disabling versioning entirely.** — Loses concurrency protection; data corruption possible. (60% fail)
