Using Python 2.3 and (debian linux) mysqldb 1.2.1c2-1:
Generated a table with a foreign key constraint, and intentionally
violated the constraint during testing. Output was:
Exception in thread Thread-1:Traceback (most recent call last):
[useless stuff removed]
File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line
137, in execute
self.errorhandler(self, exc, value)
connections.py", line 33, in defaulterrorhandler
raise errorclass, errorvalue
OperationalError: (1216, 'Cannot add or update a child row: a
foreign key constraint fails')
According to the Python DB API 2.0 (http://www.python.org/peps/
pep-0249.html), a foreign key constraint violation should throw an
IntegrityError, not an OperationalError. (relevant paragraphs
quoted below for convenience)
Exception raised for errors that are related to the database's operation and not necessarily under the control of the programmer, e.g. an unexpected disconnect
the data source name is not found, a transaction could
be processed, a memory allocation error occurred during
processing, etc. It must be a subclass of DatabaseError.
Exception raised when the relational integrity of the database is affected, e.g. a foreign key check fails. It must be a subclass of DatabaseError.
As it is, I can't tell in my code (without examining the text string
returned in the exception) whether a commit failed because of
bogus data, or because of a database disconnect, which makes it
hard to decide how to proceed... :(
(submitter: John Martinez - 'jvm at snarkhunter dot com')
Thanks for your time.
Log in to post a comment.