--- /dev/null
+.. change::
+ :tags: usecase, asyncio, postgresql
+ :tickets: 6199
+
+ Added accessors ``.sqlstate`` and synonym ``.pgcode`` to the ``.orig``
+ attribute of the SQLAlchemy exception class raised by the asyncpg DBAPI
+ adapter, that is, the intermediary exception object that wraps on top of
+ that raised by the asyncpg library itself, but below the level of the
+ SQLAlchemy dialect.
translated_error = exception_mapping[super_](
"%s: %s" % (type(error), error)
)
+ translated_error.pgcode = (
+ translated_error.sqlstate
+ ) = getattr(error, "sqlstate", None)
raise translated_error from error
else:
raise error
eq_(cparams["host"], "hostA:portA,hostB,hostC")
+class PGCodeTest(fixtures.TestBase):
+ __only_on__ = "postgresql"
+
+ def test_error_code(self, metadata, connection):
+ t = Table("t", metadata, Column("id", Integer, primary_key=True))
+ t.create(connection)
+
+ errmsg = assert_raises(
+ exc.IntegrityError,
+ connection.execute,
+ t.insert(),
+ [{"id": 1}, {"id": 1}],
+ )
+
+ if testing.against("postgresql+pg8000"):
+ # TODO: is there another way we're supposed to see this?
+ eq_(errmsg.orig.args[0]["C"], "23505")
+ else:
+ eq_(errmsg.orig.pgcode, "23505")
+
+ if testing.against("postgresql+asyncpg"):
+ eq_(errmsg.orig.sqlstate, "23505")
+
+
class ExecuteManyMode(object):
__only_on__ = "postgresql+psycopg2"
__backend__ = True