You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django模型remote字段报value too long for type character varying(3)错误

Django模型字段max_length不生效问题(PostgreSQL)

我在Django模型中以相同方式声明了两个选择字段,但remote字段选择长度超过3的选项时,会触发value too long for type character varying(3)错误,field字段无此问题。

模型代码如下:

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,)

环境信息及报错栈如下:

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)

问题原因

数据库中remote字段的实际存储长度为3,和模型定义的max_length=20不一致。常见触发场景:

  • 最初定义remote字段时max_length设为3,后续修改为20但未生成/应用迁移
  • 手动修改过PostgreSQL表结构,导致字段长度与模型定义脱节

解决步骤

  1. 验证数据库字段长度
    连接PostgreSQL执行以下SQL,查看remote字段的实际长度:
    SELECT character_maximum_length 
    FROM information_schema.columns 
    WHERE table_name = 'track_assignmentad' 
      AND column_name = 'remote';
    
  2. 生成并应用迁移
    如果查询结果为3,执行以下命令同步模型与数据库结构:
    # 生成迁移文件
    python manage.py makemigrations
    # 应用迁移到数据库
    python manage.py migrate
    
  3. 强制生成迁移(如果自动检测失败)
    若Django未检测到字段变化,可手动创建迁移文件,或先删除对应app下的旧迁移记录(注意备份数据),再重新执行上述迁移命令。

内容的提问来源于stack exchange,提问作者Windfish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 09:35:24