python data_error ai_generated true

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

ID: python/flask-sqlalchemy-integrityerror

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2025-04-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2.x active
3.x active

Root Cause

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

generic

中文

尝试向具有 UNIQUE 约束的列插入重复值。

Workarounds

  1. 85% success
    if not User.query.filter_by(email=email).first():
        db.session.add(user)
        db.session.commit()
  2. 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:

  1. 70% fail

    Silently fails to create the user, leading to inconsistent state.

  2. 90% fail

    Allows duplicate emails, breaking application logic.