Is there a way to check which column is a duplicate when entering data into a table with unique properties (sqlalchemy)

Viewed 8

I have a users table with:

id: number
username: unique string
password: string
email: unique string
first_name: string
last_name: string
...

Recently I tried to register with a user which has the same username as an already entered one and got an error. I tried to catch it as an IntegrityError exception but couldn't find any information regarding WHICH column is a duplicate in order to inform the user if it is the username, email or something else.

Is there any way to do this short of performing a query on all unique keys and seeing if they return something?

1 Answers

you can read the msg within the IntegrityError Exception

except IntegretyError as e:
    if "username" in e.message:
        pass
Related