Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django ORM: Get column of DataBaseError

Is there a way to get the relevant table and column name, and not just this message?

django.db.utils.DataError: value too long for type character varying(16)

I don't like to guess or search the relevant column.

We use django 1.6 and PostgreSQL 9.3.6

Full traceback

  File "/home/f/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/f/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/f/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/f/local/lib/python2.7/site-packages/django/db/models/base.py", line 687, in _do_insert
    using=using, raw=raw)
  File "/home/f/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/f/local/lib/python2.7/site-packages/django/db/models/query.py", line 1514, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/f/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 903, in execute_sql
    cursor.execute(sql, params)
  File "/home/f/local/lib/python2.7/site-packages/django/db/backends/util.py", line 69, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/f/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/home/f/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/f/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
django.db.utils.DataError: value too long for type character varying(16)
like image 311
guettli Avatar asked Mar 24 '15 08:03

guettli


1 Answers

There is no way to simply find out.

Django here just quotes what the DB says, and this is a fault of postgres:

db=> INSERT INTO db_accesskeys (key, link) VALUES ('AAAAA...', 'BBBBB...');
ERROR:  value too long for type character varying(40)

postgresql doesn't tell you which one is too long, so there is no way django could find out, too.

A workaround would be to validate every input manually to match the max_length (I'm not sure about the exact implementation on django 1.6 though, this is 1.8):

def validate_length(model, field, value):
    if len(value) > model._meta.get_field(field).max_length:
        raise Exception(
            field + " of " + model._meta.model_name + " is too long (" + str(len(value)) + " > " +
            str(model._meta.get_field(field).max_length) + ")"
    )


validate_length(AccessKeys, "key", "AAAAAAA...")
Exception: key of accesskeys is too long (109 > 40)

You can of course tune the thrown exception to your needs to make it easier to access the field name that's to long, or to see exactly how many chars to long it is or ...

like image 60
Chris Schäpers Avatar answered Nov 16 '22 15:11

Chris Schäpers