How to identify unique constraint violation without dependency on a specific database driver?

11 views
Skip to first unread message

Vitaly Kruglikov

unread,
Sep 18, 2020, 10:34:26 AM9/18/20
to sqlalchemy
My table has a unique index on the column named "tag". When I attempt to insert a row with a tag value that already exists in the table, sqlalchemy raises the generic exception `IntegrityError`.

`IntegrityError` may be raised for various scenarios, including Foreign Key Violation and Unique Index Violation.

One of the benefits of using sqlalchemy is the ability to write code that may be independent of the underlying backend or db-api driver. 

So, I am hoping that there is a programmatic way to distinguish between the underlying causes of IntegrityError (such as unique constraint violation) without having to depend explicitly on a specific driver package (e.g., psycopg2).

Thank you,
Vitaly

Mike Bayer

unread,
Sep 18, 2020, 3:08:34 PM9/18/20
to noreply-spamdigest via sqlalchemy


On Fri, Sep 18, 2020, at 3:34 PM, Vitaly Kruglikov wrote:
My table has a unique index on the column named "tag". When I attempt to insert a row with a tag value that already exists in the table, sqlalchemy raises the generic exception `IntegrityError`.

`IntegrityError` may be raised for various scenarios, including Foreign Key Violation and Unique Index Violation.

One of the benefits of using sqlalchemy is the ability to write code that may be independent of the underlying backend or db-api driver. 

this is true for the most part!  unfortunately, not in this very specific part :(


So, I am hoping that there is a programmatic way to distinguish between the underlying causes of IntegrityError (such as unique constraint violation) without having to depend explicitly on a specific driver package (e.g., psycopg2).

you have you set up a catch that will apply regular expressions to match the error conditions you want.  there's no platform agnostic registry maintained of these right now (it would be massive).





Thank you,
Vitaly


--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Vitaly Krug

unread,
Sep 18, 2020, 3:45:16 PM9/18/20
to sqlal...@googlegroups.com
Thank you, I was afraid of that.

Best,
Vitaly

On Fri, Sep 18, 2020, 12:08 PM Mike Bayer <mik...@zzzcomputing.com> wrote

...
Reply all
Reply to author
Forward
0 new messages