Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to access PostgreSQL SQLSTATE codes in SQLAlchemy?

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

like image 881
José María Avatar asked Feb 27 '26 04:02

José María


1 Answers

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!")
like image 127
henrycjc Avatar answered Mar 01 '26 21:03

henrycjc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!