I'm looking for a way to get the error code in a PostgreSQL exception from SQLAlchemy.
Is there a way to do it. Thanks
Yes, you can. It's available on the orig.pgcode member.
e.g.
import sqlalchemy as sa
try:
exec_database_script(...)
except sa.exc.IntegrityError as e:
pgcode = int(e.orig.pgcode)
if pgcode == 23514:
print("Check constraint violation!")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With