In one of my tests I'm checking that a pair of (non primary) fields must be 
unique. This definition is done with:

__table_args__ = (UniqueConstraint('entity_id', 'version', name=
where `entity_id` is an INT column referring to a foreign id and version is 
a plain INT column.

The way I test for it (using pytest):

from sqlalchemy.exc import IntegrityError

def test_entity_version_entity_version_combined_unique(session_with_entities
    # Ensure created object has duplicate data
    with pytest.raises(IntegrityError):

But the type of exceptions I get are of type sqlite3.IntegrityError or 
pymysql.IntegrityError. Basically the connector's version of the error.

I couldn't find a proper super class (unless you count Exception). I 
thought maybe SQLAlchemy's DBAPIError would be able to do it based on the 
documentation but it didn't cut it.

Is there a proper way to catch these implementation specific exceptions?

Kind regards,

Kevin CYT

SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See 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 post to this group, send email to
Visit this group at
For more options, visit

Reply via email to