pythondjangopostgresqldjango-modelsdjango-model-field

Django model field bug? "value too long for type character varying(3)" error


I am declaring two choice fields for my django model in the same manner but one of them somehow bugs into having maxlength=3. I am using a PostgreSQL database.

Here is how I declare the fields:

class AssignmentAd(models.Model):
    class FieldChoices(models.TextChoices):
        JFS = "JFS"
        JAVA = "Java"
        CLOUD = "Cloud"
        __empty__ = _('(Unknown)')

    class RemoteChoices(models.TextChoices):
        JFS = "JFS"
        JAVA = "Java"
        CLOUD = "Cloud"
        __empty__ = _('(Unknown)')


    remote = models.CharField(max_length=20, choices=RemoteChoices.choices, blank=True,)
    field = models.CharField(max_length=20, choices=FieldChoices.choices, blank=True,)

After declaring these two fields in my model, I can create an instance through admin by choosing "JFS" for both these fields (since it is 3 chars long) but if I choose any other option that is longer than 3 chars for the remote field, I get this "value too long for type character varying(3)" error. I don't get this error for the "field" field only for the "remote" field. What gives?

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/admin/track/assignmentad/2/change/

Django Version: 4.1
Python Version: 3.10.7
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'members',
 'track']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)

The above exception (value too long for type character varying(3)
) was the direct cause of the following exception:
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\admin\options.py", line 686, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\decorators.py", line 133, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\decorators\cache.py", line 62, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\admin\sites.py", line 242, in inner
    return view(request, *args, **kwargs)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\admin\options.py", line 1893, in change_view
    return self.changeform_view(request, object_id, form_url, extra_context)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\decorators.py", line 133, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\admin\options.py", line 1750, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\admin\options.py", line 1802, in _changeform_view
    self.save_model(request, new_object, form, not add)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\admin\options.py", line 1225, in save_model
    obj.save()
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\base.py", line 831, in save
    self.save_base(
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\base.py", line 882, in save_base
    updated = self._save_table(
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\base.py", line 995, in _save_table
    updated = self._do_update(
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\base.py", line 1059, in _do_update
    return filtered._update(values) > 0
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 1215, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\sql\compiler.py", line 1819, in execute_sql
    cursor = super().execute_sql(result_type)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\sql\compiler.py", line 1395, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 103, in execute
    return super().execute(sql, params)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 67, in execute
    return self._execute_with_wrappers(
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)

Exception Type: DataError at /admin/track/assignmentad/2/change/
Exception Value: value too long for type character varying(3)

Solution

  • Have you started by setting the Charfield max length value to 3 and modified it afterwards ?

    If you did so, then you may have applied a first migration to your database and may have forgotten to migrate the data after changing the code.

    To check if you database schema is not lagging behind your code, you can:

    1. Run python manage.py makemigrations: if a new migration file is generated, then you probably found the root cause. Open the new migration file to check the changes, and apply it using python manage.py migrate

    2. Open your PostgreSQL database using a tool like PGAdmin. Check the definition of the columns of the AssignmentAdd table