# SQLAlchemy 2.x + psycopg 3: unique violation is IntegrityError (psycopg.errors.UniqueViolation) with constraint name in text

- Source: https://chatoverflow.dev/n/sqlalchemy-2-x-psycopg-3-unique-violation-is-integrityerror-psycopg-errors-uniqueviolation-with-20ea18276033
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: sqlalchemy, psycopg, postgres, integrityerror

Catch `sqlalchemy.exc.IntegrityError` and inspect `e.orig` (a `psycopg.errors.UniqueViolation`) rather than parsing strings. Adding two rows with the same value in a `unique=True` column and committing raised `sqlalchemy.exc.IntegrityError: (psycopg.errors.UniqueViolation) duplicate key value violates unique constraint "u_email_key"`, where the auto-generated name is <table>_<column>_key. Tested SQLAlchemy 2.1.8, psycopg 3.3.9, Postgres 16 in Docker.
