#33647: bulk_update silently truncating values for size limited fields
-------------------------------------+-------------------------------------
               Reporter:  jerch      |          Owner:  nobody
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:  Database   |        Version:  4.0
  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          |
-------------------------------------+-------------------------------------
 On postgres backend, `bulk_update` passes overlong values for size limited
 fields along without any notification/exception, instead truncating the
 value.

 Repro:
 {{{
 #!div style="font-size: 80%"
 Code highlighting:
   {{{#!python
   # some model to repro
   class TestModel(models.Model):
       name = models.CharField(max_length=32)

   # in the shell
   >>> from bulk_test.models import TestModel
   >>> tm=TestModel(name='hello')
   >>> tm.save()
   >>> tm.name
   'hello'
   >>> tm.name='m'*100
   >>> tm.save()  # good, raises:
   ...
   django.db.utils.DataError: value too long for type character varying(32)

   >>> TestModel.objects.all().values('name')
   <QuerySet [{'name': 'hello'}]>
   >>> TestModel.objects.all().update(name='z'*100)  # good, raises as
 well:
   ...
   django.db.utils.DataError: value too long for type character varying(32)

   >>> TestModel.objects.all().values('name')
   <QuerySet [{'name': 'hello'}]>
   >>> TestModel.objects.bulk_update([tm], ['name'])  # not raising,
 instead truncating:
   1
   >>> TestModel.objects.all().values('name')
   <QuerySet [{'name': 'mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm'}]>
   }}}
 }}}

 Not sure, if this is intended/expected behavior, well it is inconsistent
 to `.save` or `.update`, which both raise here. I only tested postgres
 backend for this, it may apply to other size limiting databases as well
 (sqlite itself is not affected, as it does not limit values).

 If this is intended, it may be a good idea to at least document the
 slightly different behavior, so users are aware of it, and can prepare
 their code to avoid silent truncation with follow-up errors. A better way
 prolly would fix `bulk_update` to spot value overflows and raise, but I am
 not sure, if thats feasible.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33647>
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/010701802d59302e-83f4ff33-484e-4fc8-a537-bb3b508a92f9-000000%40eu-central-1.amazonses.com.

Reply via email to