#22305: MaxLengthValidator doesn't take database encoding into account
------------------------+--------------------------------------
     Reporter:  joeri   |                    Owner:  nobody
         Type:  Bug     |                   Status:  new
    Component:  Forms   |                  Version:  1.6
     Severity:  Normal  |               Resolution:
     Keywords:          |             Triage Stage:  Unreviewed
    Has patch:  0       |      Needs documentation:  0
  Needs tests:  0       |  Patch needs improvement:  0
Easy pickings:  0       |                    UI/UX:  0
------------------------+--------------------------------------
Changes (by joeri):

 * cc: joeri (added)
 * status:  closed => new
 * resolution:  worksforme =>


Comment:

 Hi,

 I was a bit incomplete in my ticket. We are using PostgreSQL 9.1 and we
 traced the problem to the database charset, which was set to `SQL_ASCII`
 (for some reason `template0` charset was set to this and used to create
 the db). You would still expect everything to work on the db end, or that
 your `ModelForm` catches the error.

 Edge case, so maybe a note in the docs would suffice.

 Here's the unit tests that I ran against a PostgreSQL db with `SQL_ASCII`
 encoding:

 {{{
 # -*- coding: utf-8 -*-
 from django.test import TestCase
 from django.db import models
 from django.forms.models import modelform_factory


 class Pizza(models.Model):
     name = models.CharField(max_length=10)  # Short pizza names ftw.


 class MyTests(TestCase):
     def test_form_to_db(self):
         form = modelform_factory(Pizza)
         pizza_name = u'mozzarélla'
         f = form(data={'name': pizza_name})

         self.assertTrue(f.is_valid())
         f.save()  # Gives an error
 }}}

 And here's the result of the test:

 {{{
 $ python src/manage.py test
 Creating test database for alias 'default'...
 E
 ======================================================================
 ERROR: test_form_to_db (tests.MyTests)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File "/home/joeri/playground/django-1.6/ticket22305/tests.py", line 18,
 in test_form
     f.save()
   File "/home/joeri/playground/django-1.6/env/local/lib/python2.7/site-
 packages/django/forms/models.py", line 446, in save
     construct=False)
   File "/home/joeri/playground/django-1.6/env/local/lib/python2.7/site-
 packages/django/forms/models.py", line 99, in save_instance
     instance.save()
   File "/home/joeri/playground/django-1.6/env/local/lib/python2.7/site-
 packages/django/db/models/base.py", line 545, in save
     force_update=force_update, update_fields=update_fields)
   File "/home/joeri/playground/django-1.6/env/local/lib/python2.7/site-
 packages/django/db/models/base.py", line 573, in save_base
     updated = self._save_table(raw, cls, force_insert, force_update,
 using, update_fields)
   File "/home/joeri/playground/django-1.6/env/local/lib/python2.7/site-
 packages/django/db/models/base.py", line 654, in _save_table
     result = self._do_insert(cls._base_manager, using, fields, update_pk,
 raw)
   File "/home/joeri/playground/django-1.6/env/local/lib/python2.7/site-
 packages/django/db/models/base.py", line 687, in _do_insert
     using=using, raw=raw)
   File "/home/joeri/playground/django-1.6/env/local/lib/python2.7/site-
 packages/django/db/models/manager.py", line 232, in _insert
     return insert_query(self.model, objs, fields, **kwargs)
   File "/home/joeri/playground/django-1.6/env/local/lib/python2.7/site-
 packages/django/db/models/query.py", line 1511, in insert_query
     return query.get_compiler(using=using).execute_sql(return_id)
   File "/home/joeri/playground/django-1.6/env/local/lib/python2.7/site-
 packages/django/db/models/sql/compiler.py", line 899, in execute_sql
     cursor.execute(sql, params)
   File "/home/joeri/playground/django-1.6/env/local/lib/python2.7/site-
 packages/django/db/backends/util.py", line 53, in execute
     return self.cursor.execute(sql, params)
   File "/home/joeri/playground/django-1.6/env/local/lib/python2.7/site-
 packages/django/db/utils.py", line 99, in __exit__
     six.reraise(dj_exc_type, dj_exc_value, traceback)
   File "/home/joeri/playground/django-1.6/env/local/lib/python2.7/site-
 packages/django/db/backends/util.py", line 53, in execute
     return self.cursor.execute(sql, params)
 DataError: value too long for type character varying(10)
 }}}

 Reopening the ticket.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/22305#comment:2>
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/063.d1f2e17e456c3565d9dfbed41f9a6f9c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to