I have this code:
try:
parent_comment = models.Comment.all_objects.get(id=parent_comment_id)
except models.Comment.DoesNotExist:
parent_comment = None
if parent_comment is not None and parent_comment_id is None:
raise Exception("WTF django/mysql")
… and sometimes, the exception somehow gets raised. How could this ever happen?
Once in a while, several times a day, it returns seemingly random Comment instances. Usually it behaves as expected and returns None.
This is the id field of the Comment table: id int(11) NOT NULL AUTO_INCREMENT so it’s not that it’s nullable. It’s an InnoDB table.
As for Comment.all_objects, this is its definition: all_objects = Manager() and it’s the first line in that class.
We’re on Django 1.2.7.
Update
Added logging to the exception to get the SQL that’s generated when the exception is raised. Here it is:
SELECT `canvas_comment`.`id`, `canvas_comment`.`visibility`, `canvas_comment`.`parent_content_id`, `canvas_comment`.`parent_comment_id`, `canvas_comment`.`timestamp`, `canvas_comment`.`reply_content_id`, `canvas_comment`.`reply_text`, `canvas_comment`.`replied_comment_id`, `canvas_comment`.`category_id`, `canvas_comment`.`author_id`, `canvas_comment`.`title`, `canvas_comment`.`ip`, `canvas_comment`.`anonymous`, `canvas_comment`.`score`, `canvas_comment`.`judged`, `canvas_comment`.`ot_hidden` FROM `canvas_comment` WHERE `canvas_comment`.`id` IS NULL
This behaviour is caused by deeply strange (in this coder’s humble opinion) MySQL behaviour, controlled by the
SQL_AUTO_IS_NULLvariable (which is1by default in MySQL < 5.5.3):There is a Django bug (closed: wontfix) describing similar confusion caused by this “feature”, in which a core developer states
The solution, then, is to disable the
SQL_AUTO_IS_NULLoption of your MySQL database using theSETstatement. You can do this insettings.pywith something like:Longer-term, you can try and drum-beat on the django-developers list to get them to reconsider their earlier position: