# sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper Mapper[User(users)] or this Session.

- **ID:** `python/pytest-sqlalchemy-session-not-bound`
- **Domain:** python
- **Category:** config_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

A SQLAlchemy Session was created without an engine (or sessionmaker bound to engine) and the test fixture yields a session that is later used against an unbound mapper.

## Version Compatibility

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

## Workarounds

1. **** (96% success)
   ```
   from sqlalchemy.orm import sessionmaker

@pytest.fixture
def db(engine):
    Session = sessionmaker(bind=engine)
    session = Session()
    yield session
    session.close()
   ```
2. **** (93% success)
   ```
   from sqlalchemy.orm import scoped_session, sessionmaker

@pytest.fixture
def db(engine):
    factory = sessionmaker(bind=engine)
    Session = scoped_session(factory)
    yield Session()
    Session.remove()
   ```

## Dead Ends

- **** — Rebinding after the session has already executed queries or checked out connections raises InvalidRequestError; also fragile with scoped_session. (80% fail)
- **** — session.connection() requires a bind in the first place; it raises the same UnboundExecutionError. (92% fail)
