[Solved]-Django : Deduce duplicate key exception from IntegrityError

10👍

Based on the comment from @karthikr, I found the error type:

type(e.__cause__)
<class 'psycopg2.IntegrityError'>

A little poking around showed this:

e.__cause__.pgcode
'23505'

My understanding is, as long as I stick with the same database I can check this to verify that it’s a duplicate key error.

& psycopg2 doesn’t change the error code,

👤user

Leave a comment