Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a Django web site running a mini CMS we've built internally years ago, it's using postgresql. When saving a simple title and a paragraph of text I get the following error:

value too long for type character varying(100)

The weird thing is, not a single column is varying(100) they are all 200 or 250, even the default Django ones have been changed from the 100 to 200 due to a re-opened ticket mentioned here

Does anyone know of a solution to this problem?

like image 391
tdelam Avatar asked Jan 27 '12 15:01

tdelam


People also ask

What is character varying in PostgreSQL?

The notations varchar( n ) and char( n ) are aliases for character varying( n ) and character( n ) , respectively. character without length specifier is equivalent to character(1) . If character varying is used without length specifier, the type accepts strings of any size. The latter is a PostgreSQL extension.

How do I increase the size of a column in PostgreSQL?

How to increase the length of a character varying datatype in Postgres without data loss. Run the following command: alter table TABLE_NAME alter column COLUMN_NAME type character varying(120); This will extend the character varying column field size to 120.

What is varchar in PostgreSQL?

PostgreSQL supports a character data type called VARCHAR. This data type is used to store characters of limited length. It is represented as varchar(n) in PostgreSQL, where n represents the limit of the length of the characters. If n is not specified it defaults to varchar which has unlimited length.


7 Answers

I can bet money you have a models.SlugField without length set. The default length is 50 characters, most likely it's not enough for your use case.

Change it to models.SlugField(max_length=255) and migrate your database schema.

like image 55
Michael Samoylov Avatar answered Sep 26 '22 22:09

Michael Samoylov


I also had this problem when using a filefield and was scratching my head for a while. Of course the default FileField instances are created with a 100 character limit.

https://docs.djangoproject.com/en/dev/ref/models/fields/#filefield

like image 34
Kerridge0 Avatar answered Sep 23 '22 22:09

Kerridge0


This is an error message from Postgres and not django.

You seem to have changed the length of the field in the models.py, but that doesn't change the database length which was created when you did a manage.py syncdb.

You have to alter the length of the field in the database, directly.

like image 21
lprsd Avatar answered Sep 25 '22 22:09

lprsd


Django 2.1

I encountered this problem while switching from sqlite3 to postgresql. Remove the migration files in each app's migrations folder except __init__.py Then re-run migration

(venv)myapp$python manage.py makemigrations
(venv)myapp$python manage.py migrate
(venv)myapp$python manage.py runserver
like image 26
7guyo Avatar answered Sep 24 '22 22:09

7guyo


I had a similar problem with django-autoslugfield I was using a similar package and then switched over to django-autoslugfield

I was getting this error: value too long for type character varying(50)

despite the fact that my models.py had:

slug = AutoSlugField(max_length=255, populate_from='name', unique=True)

and in my db the it the type was character varying 255

once i remove max_length=255 from the field i.e.

slug = AutoSlugField(populate_from='name', unique=True)

then it worked fine

like image 22
lukeaus Avatar answered Sep 26 '22 22:09

lukeaus


Michael Samoylov's answer pointed me in the right direction. I had the same error come up, except it was with a FileField.

Fields have a max_length, even if you have not explicitly set a max_length. Increase the value so that your data fits to avoid the error.

In my case, the data was too large to reasonably store in the database. I resorted to saving my file to disk, then saving the file path in the database.

like image 34
Shaun Overton Avatar answered Sep 23 '22 22:09

Shaun Overton


i went through this same error. and when i made changes in the modele, i kept having the same error.

Here is how i fixed it. It might be necessary to skip few migrations for the program to only use the migration where changes have been made for the CharField max_lenght.

for that you have to run

python manage.py showmigrations 

to see which migrations have not been made.

then you skip them until you get to the last with the command

python manage.py migrate <app> 000_migration_number --fake 
like image 27
Romain BOBOE Avatar answered Sep 24 '22 22:09

Romain BOBOE