# sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: users.email

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

## Root Cause

Attempt to insert a duplicate value into a column with a UNIQUE constraint.

## Version Compatibility

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

## Workarounds

1. **Check for existing record before insertion** (95% success)
   ```
   existing = User.query.filter_by(email=email).first()
if not existing:
    db.session.add(new_user)
    db.session.commit()
   ```
2. **Use save-or-update logic** (90% success)
   ```
   user = User.query.filter_by(email=email).first()
if user:
    user.name = name
else:
    user = User(email=email, name=name)
db.session.add(user)
db.session.commit()
   ```

## Dead Ends

- **Ignoring the error and retrying the commit** — Same error will occur if duplicate data is not resolved. (95% fail)
- **Removing the UNIQUE constraint from the database** — Compromises data integrity; allows duplicate emails. (60% fail)
