#13666: Updates with F objects on decimal fields raise MySQL warnings
------------------------------------+---------------------------------------
          Reporter:  KyleMac        |         Owner:  nobody
            Status:  reopened       |     Milestone:        
         Component:  Documentation  |       Version:  1.2   
        Resolution:                 |      Keywords:        
             Stage:  Unreviewed     |     Has_patch:  0     
        Needs_docs:  0              |   Needs_tests:  0     
Needs_better_patch:  0              |  
------------------------------------+---------------------------------------
Comment (by KyleMac):

 Assuming that the field "balance" is a decimal field with 2 decimal
 places, in raw SQL the following will show the truncation warning:

 {{{
 UPDATE ticket SET balance = 10.00;
 UPDATE ticket SET balance = balance - '1.79';
 SHOW WARNINGS;
 }}}

 However, the following will work fine:
 {{{
 UPDATE ticket SET balance = 10.00;
 UPDATE ticket SET balance = balance - 1.79;
 SHOW WARNINGS;
 }}}

 It seems that MySQL doesn't like strings as much as it likes floats and
 Django converts Decimal() to strings. So

 {{{
 Account.objects.create(name='z1', balance='10.00')
 Account.objects.filter(name__startswith='z').update(balance=F('balance') -
 Decimal('1.79'))
 }}}

 or

 {{{
 Account.objects.create(name='z1', balance='10.00')
 Account.objects.filter(name__startswith='z').update(balance=F('balance') -
 '1.79')
 }}}

 raise exceptions, but

 {{{
 Account.objects.create(name='z1', balance='10.00')
 Account.objects.filter(name__startswith='z').update(balance=F('balance') -
 float(Decimal('1.79')))
 }}}

 works fine.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/13666#comment:3>
Django <http://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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to