# sqlalchemy.exc.IntegrityError: (psycopg2.errors.UniqueViolation) duplicate key value violates unique constraint "users_email_key"

- **ID:** `python/pytest-sqlalchemy-fixture-rollback`
- **Domain:** python
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

The database fixture does not roll back transactions between tests, causing data from previous tests to persist.

## Version Compatibility

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

## Workarounds

1. **** (95% success)
   ```
   @pytest.fixture(scope='function')
def db_session():
    connection = engine.connect()
    transaction = connection.begin()
    session = Session(bind=connection)
    yield session
    session.close()
    transaction.rollback()
    connection.close()
   ```
2. **** (90% success)
   ```
   Use pytest-sqlalchemy's fixtures or factory_boy with a session that rolls back after each test.
   ```

## Dead Ends

- **** — This is a workaround that does not fix the underlying state leakage and may lead to other issues. (70% fail)
- **** — This is slow and may not be feasible for large test suites. (80% fail)
