Wrong exception type generated
MySQL database connector for Python programming
Brought to you by:
adustman
According to the Python DB API 2.0 (http://legacy.python.org/dev/peps/pep-0249/#integrityerror), an IntegrityError should be raised when a required (NOT NULL) parameter is missing, but instead an OperationalError is raised.
Tested with mysql-python 1.2.3.
import MySQLdb
import MySQLdb.cursors
db = MySQLdb.connect(host="localhost", user="root", db="test", sql_mode="traditional")
db.autocommit(False)
cur = db.cursor()
cur.execute("DROP TABLE IF EXISTS `test`")
cur.execute("CREATE TABLE `test` (`integer` bigint(20) NOT NULL, `desc` varchar(255) NOT NULL)")
db.commit()
cur.execute("INSERT INTO `test` (`integer`) VALUES (%s)", (1, ))
db.commit()
$ python /tmp/test.py
Traceback (most recent call last):
File "/tmp/test.py", line 12, in <module>
cur.execute("INSERT INTO `test` (`integer`) VALUES (%s)", (1, ))
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1364, "Field 'desc' doesn't have a default value")
Related and closed bugs:
https://sourceforge.net/p/mysql-python/bugs/11/
https://sourceforge.net/p/mysql-python/bugs/157/
Github pull request:
https://github.com/farcepest/MySQLdb1/pull/78