[Django]-DatabaseError: value too long for type character varying(100)

124๐Ÿ‘

โœ…

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.

41๐Ÿ‘

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

๐Ÿ‘คKerridge0

18๐Ÿ‘

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.

๐Ÿ‘คlprsd

5๐Ÿ‘

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
๐Ÿ‘ค7guyo

2๐Ÿ‘

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

๐Ÿ‘คlukeaus

2๐Ÿ‘

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 
๐Ÿ‘คRomain BOBOE

1๐Ÿ‘

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

๐Ÿ‘คRamon de Jesus

1๐Ÿ‘

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.

๐Ÿ‘คShaun Overton

1๐Ÿ‘

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.

๐Ÿ‘คapet

1๐Ÿ‘

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.

0๐Ÿ‘

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.

๐Ÿ‘คBicameral Mind

0๐Ÿ‘

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 ๐Ÿ™‚

๐Ÿ‘คAnnaleise

0๐Ÿ‘

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

๐Ÿ‘คRabeul Hasan

0๐Ÿ‘

its just enough to remove max_lenght from password field .

๐Ÿ‘คAli Ghorbani

-1๐Ÿ‘

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

Leave a comment