python
data_error
ai_generated
true
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: user.email
ID: python/flask-sqlalchemy-integrityerror
80%Fix Rate
88%Confidence
0Evidence
2025-04-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 2.x | active | — | — | — |
| 3.x | active | — | — | — |
Root Cause
Attempting to insert a duplicate value into a column with a UNIQUE constraint.
generic中文
尝试向具有 UNIQUE 约束的列插入重复值。
Workarounds
-
85% success
if not User.query.filter_by(email=email).first(): db.session.add(user) db.session.commit() -
90% success
from sqlalchemy.dialects.postgresql import insert stmt = insert(User).values(email=email).on_conflict_do_nothing()
Dead Ends
Common approaches that don't work:
-
70% fail
Silently fails to create the user, leading to inconsistent state.
-
90% fail
Allows duplicate emails, breaking application logic.