# sqlalchemy.exc.InvalidRequestError: Session is already flushing

- **ID:** `python/flask-sqlalchemy-session-scope`
- **Domain:** python
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Trying to commit or flush a session while it's already in the process of flushing, often due to nested transactions.

## Version Compatibility

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

## Workarounds

1. **** (95% success)
   ```
   Ensure one commit per request: use Flask-SQLAlchemy's teardown to remove session.
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
# In route: db.session.add(obj); db.session.commit() only once
   ```
2. **** (85% success)
   ```
   Use session.begin_nested() for subtransactions and commit at end.
   ```

## Dead Ends

- **** — Exacerbates nested transaction issues. (80% fail)
- **** — May lose changes and not solve the underlying concurrency issue. (60% fail)
