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

**URL:** <https://forum.djangoproject.com/t/django-db-utils-dataerror-value-too-long-for-type-character-varying-7/13048>\
**Category:** Using the ORM\
**Created:** [April 7, 2022, 10:42am UTC](https://forum.djangoproject.com/t/django-db-utils-dataerror-value-too-long-for-type-character-varying-7/13048 "2022-04-07T10:42:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SeritiAymen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/seritiaymen/32/7933_2.png) [@SeritiAymen](https://forum.djangoproject.com/u/SeritiAymen)\
**Post date:** [April 7, 2022, 10:42am UTC](https://forum.djangoproject.com/t/django-db-utils-dataerror-value-too-long-for-type-character-varying-7/13048/1 "2022-04-07T10:42:23Z")

</div>

Everything was working right, at a certain moment I changer the max\_length for a CharField to 7, and it gave me this error when I try to migrate:

```auto
django.db.utils.DataError: value too long for type character varying(7)

```

Now that I’ve canceled the changes, when I try to migrate, I keep getting the same error, ANY SOLUTIONS PLEASE ?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 7, 2022, 11:02am UTC](https://forum.djangoproject.com/t/django-db-utils-dataerror-value-too-long-for-type-character-varying-7/13048/2 "2022-04-07T11:02:59Z")

</div>

First, check to see if that migration was applied. ([manage.py showmigrations](https://docs.djangoproject.com/en/4.0/ref/django-admin/#showmigrations)). If it wasn’t (and I’m expecting it wasn’t), I believe the easiest solution would be for you to edit that migration file and change that migration to a more appropriate value for that field.

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [April 7, 2022, 12:57pm UTC](https://forum.djangoproject.com/t/django-db-utils-dataerror-value-too-long-for-type-character-varying-7/13048/3 "2022-04-07T12:57:39Z")

</div>

The error is due to data in your database being longer than 7 characters. You may want to look at the data and consider fixing it or whether 7 characters is really the limit you want.

---

<div class="post-metadata">

**Author:** ![plopidou](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/plopidou/32/5148_2.png) [@plopidou](https://forum.djangoproject.com/u/plopidou)\
**Post date:** [April 7, 2022, 12:59pm UTC](https://forum.djangoproject.com/t/django-db-utils-dataerror-value-too-long-for-type-character-varying-7/13048/4 "2022-04-07T12:59:19Z")

</div>

one of your rows has that colum at 8 characters or more.  
Make sure none of them do before migrating. Potentially by listing of the values for all your objects, something like (extremely quick and dirty):

```auto
objs = Model.objects.all().values(
    'pk',
    'your_problematic_field'
)
for obj in objs:
    l = len(obj['your_problematic_field'])
    if len(l) > 7:
        print(f'obj with id {obj["pk"]} has that field at {l} characters long')

```

do indeed run `manage.py showmigrations`

if your migration does show up, you will I think need to manually remove it from the table `django_migrations`. A simple `DELETE FROM` is quick and easy.

Couldn’t help it, better way: 🙂

```auto
from django.db.models.functions import Length
objs = Model.objects.annotate(
    your_problematic_field_length=Length('your_problematic_field')
).filter(
    your_problematic_field_length__gt=7
).values(
    'pk',
    'your_problematic_field',
    'your_problematic_field_length',
)
for obj in objs:
    print(f'obj with id {obj["pk"]} has that field at {obj["your_problematic_field_length"]} characters long')

```
