#13666: Updates with F objects on decimal fields raise MySQL warnings
-------------------------------------+-------------------------------------
     Reporter:  KyleMac              |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  1.7
  (models, ORM)                      |               Resolution:
     Severity:  Normal               |  worksforme
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

 * status:  new => closed
 * resolution:   => worksforme


Comment:

 I'm not able to reproduce anymore on MySQL 8 strict mode (minimum version
 planned for 4.2) so they must have sorted out their decimal arithmetic
 over the years so even if the `Cast` could be added by
 `CombinedExpression` or even by MySQL's `combine_expression` I don't think
 it's worth doing at this point.

 Closing as ''worksforme'' as demonstrated by this test.

 {{{#!diff
 diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py
 index 9a64d51766..2a9bcd95cc 100644
 --- a/tests/model_fields/models.py
 +++ b/tests/model_fields/models.py
 @@ -16,6 +16,10 @@
  except ImportError:
      Image = None

 +class Account(models.Model):
 +    name = models.CharField(max_length=100)
 +    balance = models.DecimalField(decimal_places=2, max_digits=12)
 +

  class Foo(models.Model):
      a = models.CharField(max_length=10)
 diff --git a/tests/model_fields/test_decimalfield.py
 b/tests/model_fields/test_decimalfield.py
 index 912d55af72..a45e634ff4 100644
 --- a/tests/model_fields/test_decimalfield.py
 +++ b/tests/model_fields/test_decimalfield.py
 @@ -3,10 +3,10 @@

  from django.core import validators
  from django.core.exceptions import ValidationError
 -from django.db import models
 +from django.db import models, connection
  from django.test import TestCase

 -from .models import BigD, Foo
 +from .models import Account, BigD, Foo


  class DecimalFieldTests(TestCase):
 @@ -113,3 +113,14 @@ def test_roundtrip_with_trailing_zeros(self):
          obj = Foo.objects.create(a="bar", d=Decimal("8.320"))
          obj.refresh_from_db()
          self.assertEqual(obj.d.compare_total(Decimal("8.320")),
 Decimal("0"))
 +
 +    def test_update_difference(self):
 +        acc = Account.objects.create(balance="10.00")
 +        acc.balance = models.F("balance") - "1.79"
 +        acc.save()
 +        acc.refresh_from_db()
 +        self.assertEqual(acc.balance, Decimal("8.21"))
 +        with connection.cursor() as cursor:
 +            cursor.execute("SHOW WARNINGS")
 +            self.assertIsNone(cursor.fetchone())
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/13666#comment:14>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701829f0dfbee-b3913ca7-2d1f-412a-beb3-ea583193ea3d-000000%40eu-central-1.amazonses.com.

Reply via email to