#28329: Wrong type conversion with MySQL 5.7
-------------------------------------+-------------------------------------
               Reporter:             |          Owner:  nobody
  dapicester                         |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  1.10
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Hi,

 I found that the wrong SQL is generated for MySQL 5.7 when using
 `django.db.models.functions.Cast`.

 Suppose I have a model:
 {{{
 class Model(models.Model):
     id = models.AutoField()
     string = models.CharField()
 }}}
 This works correctly, cast a string to integer:
 {{{
 print Model.objects.annotate(as_int=functions.Cast('string',
 models.IntegerField())).query
 SELECT `Model`.`id`, `Model`.`string`, CAST(`Model`.`string`) AS signed
 integer) AS `as_int` FROM `Model`
 }}}

 But if cast to unsigned integer (which is what I need):
 {{{
 print Model.objects.annotate(as_int=functions.Cast('string',
 models.PositiveIntegerField())).query
 SELECT `Model`.`id`, `Model`.`string`, CAST(`Model`.`string`) AS integer
 UNSIGNED) AS `as_int` FROM `Model`
 }}}

 Here is the error:
 {{{
 Model.objects.annotate(as_int=functions.Cast('string',
 models.PositiveIntegerField()))
 ProgrammingError: (1064, "You have an error in your SQL syntax; check the
 manual that corresponds to your MySQL server version for the right syntax
 to use near 'integer UNSIGNED) AS `id_int` FROM `Model` at line 1")
 }}}

 So it seems that casting to `IntegerField` uses the correct MySQL type
 `signed integer` but casting to `PositiveInteger` uses the wrong type
 `integer unsigned` (it should be `unsigned integer`, see the MySQL
 documentation at https://dev.mysql.com/doc/refman/5.7/en/cast-
 functions.html#function_cast).

--
Ticket URL: <https://code.djangoproject.com/ticket/28329>
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/053.12672af8c8dbbb40516fed8bd007142c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to