diff --git a/doc/src/extensions.rst b/doc/src/extensions.rst index c810ae81..6df90fd2 100644 --- a/doc/src/extensions.rst +++ b/doc/src/extensions.rst @@ -165,9 +165,9 @@ functionalities defined by the |DBAPI|_. table_name A string with the error field if available; `!None` if not available. - The attribute value is available only if the error sent by the server - includes the specified field and should remain available until the - cursor that generated the exception executes another query. + The attribute value is available only if the error sent by the server: + not all the fields are available for all the errors and for all the + server versions. .. autofunction:: set_wait_callback(f) diff --git a/tests/test_module.py b/tests/test_module.py index 1acdb84e..1ddc14a5 100755 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -221,6 +221,38 @@ class ExceptionsTestCase(unittest.TestCase): self.assertEqual(diag.sqlstate, '42P01') + def test_diagnostics_independent(self): + cur = self.conn.cursor() + try: + cur.execute("l'acqua e' poca e 'a papera nun galleggia") + except Exception, exc: + diag1 = exc.diag + + self.conn.rollback() + + try: + cur.execute("select level from water where ducks > 1") + except psycopg2.Error, exc: + diag2 = exc.diag + + self.assertEqual(diag1.sqlstate, '42601') + self.assertEqual(diag2.sqlstate, '42P01') + + def test_diagnostics_from_commit(self): + cur = self.conn.cursor() + cur.execute(""" + create temp table test_deferred ( + data int primary key, + ref int references test_deferred (data) + deferrable initially deferred) + """) + cur.execute("insert into test_deferred values (1,2)") + try: + self.conn.commit() + except psycopg2.Error, exc: + e = exc + self.assertEqual(e.diag.sqlstate, '23503') + @skip_before_postgres(9, 3) def test_9_3_diagnostics(self): cur = self.conn.cursor()