#27979: Bug: Using F() with mysql can leak an unwrapped OperationalError
-------------------------------------+-------------------------------------
               Reporter:  Chris      |          Owner:  nobody
  Dary                               |
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:             |        Version:  1.10
  Uncategorized                      |       Keywords:  db, mysql,
               Severity:  Normal     |  exceptions, OperationalError
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Hey folks - I'm using MySQL 5.7, python 3.6, Django 1.10.

 I just ran into this one: It looks like with a PositiveIntegerField it's
 possible to get django to leak a raw OperationalError from mysql rather
 than a wrapped one as it's intended if you try to set it below zero. I
 assume this is a specific case of a more general issue.

 Here's a sample model:

 {{{#!python
 from django.db import models

 class SampleModel(models.Model):
     num = models.PositiveIntegerField()
 }}}

 And here's some example code showing it leaking a mysql exception which is
 not trappable by the traditional django.db exceptions (in shell_plus to
 show the queries).

 {{{#!python
 In [1]: import sys
    ...: from django.db import models
    ...:

 In [2]: sample = SampleModel.objects.create(num=0)
    ...:
 INSERT INTO `users_samplemodel` (`num`)
 VALUES (0)

 Execution time: 0.000492s [Database: default]


 In [3]: try:
    ...:     sample.num = models.F('num') - 1
    ...:     sample.save()
    ...: except:
    ...:     err_type, error, traceback = sys.exc_info()
    ...:     print("Caught Exception of type: %s" % err_type)
    ...:     print("Error: %s" % error)
    ...:
 UPDATE `users_samplemodel`
 SET `num` = (`users_samplemodel`.`num` - 1)
 WHERE `users_samplemodel`.`id` = 2

 Execution time: 0.000409s [Database: default]

 Caught Exception of type: <class '_mysql_exceptions.OperationalError'>
 Error: (1690, "BIGINT UNSIGNED value is out of range in
 '(`limbo`.`users_samplemodel`.`num` - 1)'")
 }}}

 Looks like this just needs to be trapped and wrapped somewhere?

 I was able to work around it just by using a transaction and checking
 exists() first, but thought it'd be helpful to report.

 Thanks for all your work on Django!

--
Ticket URL: <https://code.djangoproject.com/ticket/27979>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.1b9a1d43939e558d609f8cd9e36e14eb%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to