Django failed: "DatabaseError: value too long for character type (50)"

I have a fixture (json) that loads into the development environment but does not work in the server environment. The error says: " DatabaseError: value too long for type character varying(50) "

My development environment is Windows and Postgres 8.4. The server is running Debian and Postgres 8.3. Database encoding is UTF8 on both systems.

It is as if the unicode markers in the device are considered characters on the server, and they cause some lines to exceed the maximum field length. However, this does not happen in the dev environment.

+12
django postgresql utf-8
Sep 27 '10 at 12:02
source share
6 answers

Well, what distinguishes the encoding of database templates. They had ascii encoding on the production server, and utf-8 on dev-dev.

By default, postgres creates the database using template1. I understand that if its encoding is not utf-8, then the database you are creating will have this problem, even if you create it using utf-8 encoding.

So I dropped it and recreated it with its encoding set to UTF8. Below is the snippet below (taken from here ):

 psql -U postgres UPDATE pg_database SET datallowconn = TRUE where datname = 'template0'; \c template0 UPDATE pg_database SET datistemplate = FALSE where datname = 'template1'; drop database template1; create database template1 with template = template0 encoding = 'UNICODE'; UPDATE pg_database SET datistemplate = TRUE where datname = 'template1'; \c template1 UPDATE pg_database SET datallowconn = FALSE where datname = 'template0'; 

Now the lamp loads smoothly.

+7
Oct 10 2018-10-10T00:
source share

Update: 50 char limit is now 255 in Django 1.8

-

Original answer:

I just ran into this this afternoon, and I have a fix.

This post here implied a Django error related to the length of the value allowed for auth_permission. Further copying supports this idea, as does this Django ticket (although it was originally associated with MySQL).

Basically, the permission name is created based on the verbose_name model plus a descriptive permission string and can overflow up to more than 50 characters allowed in auth.models.Permission.name.

To quote a comment on a Django ticket:

The longest prefixes for the string value in the auth_permission.name column are "Can change" and "Can delete", both with 11 characters. The maximum column length is 50, so the maximum length of Met.verbose_name is 39.

One solution would be to hack this column so that it supports> 50 characters (ideally, through southern migration, I say to make it easy to reproduce), but the fastest and most reliable fix I could think of, the long verbose_name definition is much shorter (from 47 characters in verbose_name to about 20). Everything works perfectly.

+10
Sep 27 '10 at 17:13
source share

Get the real SQL query on both systems and see what's different.

+1
Sep 27 '10 at 12:05
source share

For information only: I also had this error

 DatabaseError: value too long for type character varying(10) 

It seems that I wrote data for a limit of 10 for the field. I fixed it by increasing the size of CharField from 10 to 20

I hope this helps

+1
May 11 '12 at 20:40
source share

As @stevejalim says, it is entirely possible that the auth_permission.name column is a problem with a length of 50, you check this with \ d + auth_permission in the postgres shell. In my case, this is a problem, so when I load the django models, I got "DatabaseError: value too long for a variable of type (50)", then change django.contrib.auth. The resolution model is complex, therefore ... a simple solution migrated to the Permission model, I ALTER TABLE auth_permission ALTER COLUMN name TYPE VARCHAR(100); this command ALTER TABLE auth_permission ALTER COLUMN name TYPE VARCHAR(100); in the postgres shell, this works for me.

loans for this comment

+1
Jan 01 '14 at 8:22
source share

You can force Django to use longer fields for this model, decapitating the model before using it to create database tables. In "manage.py" change:

 if __name__ == "__main__": execute_manager(settings) 

at

 from django.contrib.auth.models import Permission if __name__ == "__main__": # Patch the field width to allow for our long model names Permission._meta.get_field('name').max_length=200 Permission._meta.get_field('codename').max_length=200 execute_manager(settings) 

This changes the parameters in the field to (say) manage.py syncdb , so the data table has nice wide varchar () fields. You do not need to do this when calling your application, since you never try to change the table of permissions for use.

+1
Feb 05 '15 at 11:40
source share



All Articles