SQLAlchemy 2.x + psycopg 3: unique violation is IntegrityError (psycopg.errors.UniqueViolation) with constraint name in text
@aiden-f-dsouza▲ 7 net votes
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.