jeffci
jeffci

Reputation: 2622

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?

Upvotes: 54

Views: 93406

Answers (16)

Anurag Singh
Anurag Singh

Reputation: 1

To resolve this, I manually intervened in the migrations file. I adjusted the max_length for the affected field to accommodate the larger data size (in this case, from 50 to 255). By doing this, I was able to successfully migrate both changes without any issues.

Steps Taken:

Go to the migrations file associated with your app.
Locate the migration where you increased the max_length.
In that migration, find the field in question and adjust its max_length to a value that accommodates your data size (e.g., from 50 to 255).

Upvotes: 0

Ali Ghorbani
Ali Ghorbani

Reputation: 21

its just enough to remove max_lenght from password field .

Upvotes: 2

Loránd Péter
Loránd Péter

Reputation: 314

I had this problem when I wanted to set max_length to a lower value on a FileField.
Interestingly, the error message states value too long but in my case the value was too short.

The solution was to set back the max_length to its old value. It's quite weird that the migration couldn't be done.
I also had to delete the wrongly generated migrations files and rerun python manage.py makemigrations and python manage.py migrate.

Upvotes: 1

apet
apet

Reputation: 1098

For the FileField and the ImageField, from the Django docs:

FileField instances are created in your database as varchar columns with a default max length of 100 characters. As with other fields, you can change the maximum length using the max_length argument.

Upvotes: 1

Saikat Mukherjee
Saikat Mukherjee

Reputation: 550

The value can be 100 or 25 or 17 whatever , the reason behind that is models , search where you have added that particular length(17,25) and you will get the bug ! ,

You are trying to impose a field length more than mentioned one.

This solved my issue , I hope it will help you too

Upvotes: -1

Michael Samoylov
Michael Samoylov

Reputation: 3089

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.

Upvotes: 131

Rabeul Hasan
Rabeul Hasan

Reputation: 79

predefined fields in model.py creates the problem. Extend it to desired length, i think problem will be resolved.

Upvotes: 0

Romain BOBOE
Romain BOBOE

Reputation: 377

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 

Upvotes: 2

Annaleise
Annaleise

Reputation: 51

First, try setting max_length to something reasonable on all applicable field types in your model.

For example: MyText = models.CharField(max_length=2000)

If you don't set a max_length, your database might be applying a default max_length, shorter than the length of your input data, causing your value too long for type character error.

If that doesn't work and you started in SQLite and changed databases to PostgreSQL, the previous migrations from SQLite might be interfering with the new PostgreSQL migrations.

Go into the migrations folder for your project and delete the old migration files to get a fresh start. Then try makemigrations and migrate again :)

Upvotes: 0

Salad.Guyo
Salad.Guyo

Reputation: 3425

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

Upvotes: 5

Bicameral Mind
Bicameral Mind

Reputation: 789

If it's not SlugField, FileField, or any other field mentioned here--scroll back to where the migration got stuck in the terminal. For me it was AddField

Good talk.

Upvotes: 0

Shaun Overton
Shaun Overton

Reputation: 621

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.

Upvotes: 1

lukeaus
lukeaus

Reputation: 12265

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

Upvotes: 2

Ramon de Jesus
Ramon de Jesus

Reputation: 788

I realize the question is already answered but for others that come here when looking for the error message:

In my case the problem was that my table name exceeded 50 characters. Apparently this is not allowed. Changing the table name solved the problem.

Read more here: https://code.djangoproject.com/ticket/18959

Upvotes: 1

Kerridge0
Kerridge0

Reputation: 2067

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

Upvotes: 44

lprsd
lprsd

Reputation: 87215

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.

Upvotes: 18

Related Questions