报错django.db.utils.DataError: (1406, "Data too long for column 'gender' at row 1")的解决办法

参考解决方案

这里写图片描述

Traceback (most recent call last):
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\backends\mysql\base.py", line 110, in execute
    return self.cursor.execute(query, args)
  File "C:\Users\rHotD\AppData\Roaming\Python\Python35\site-packages\pymysql\cursors.py", line 166, in execute
    result = self._query(query)
  File "C:\Users\rHotD\AppData\Roaming\Python\Python35\site-packages\pymysql\cursors.py", line 322, in _query
    conn.query(q)
  File "C:\Users\rHotD\AppData\Roaming\Python\Python35\site-packages\pymysql\connections.py", line 856, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "C:\Users\rHotD\AppData\Roaming\Python\Python35\site-packages\pymysql\connections.py", line 1057, in _read_query_result
    result.read()
  File "C:\Users\rHotD\AppData\Roaming\Python\Python35\site-packages\pymysql\connections.py", line 1340, in read
    first_packet = self.connection._read_packet()
  File "C:\Users\rHotD\AppData\Roaming\Python\Python35\site-packages\pymysql\connections.py", line 1014, in _read_packet
    packet.check_error()
  File "C:\Users\rHotD\AppData\Roaming\Python\Python35\site-packages\pymysql\connections.py", line 393, in check_error
    err.raise_mysql_exception(self._data)
  File "C:\Users\rHotD\AppData\Roaming\Python\Python35\site-packages\pymysql\err.py", line 107, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.DataError: (1406, "Data too long for column 'gender' at row 1")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pycharm\django_manage.py", line 43, in <module>
    run_module(manage_file, None, '__main__', True)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\runpy.py", line 205, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:/Users/rHotD/Documents/GitHub/Online-education-platform-Imooc/MxOnline\manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 63, in execute
    return super(Command, self).execute(*args, **options)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 183, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\contrib\auth\models.py", line 168, in create_superuser
    return self._create_user(username, email, password, **extra_fields)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\contrib\auth\models.py", line 151, in _create_user
    user.save(using=self._db)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\contrib\auth\base_user.py", line 80, in save
    super(AbstractBaseUser, self).save(*args, **kwargs)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\base.py", line 796, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\base.py", line 824, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\base.py", line 908, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\base.py", line 947, in _do_insert
    using=using, raw=raw)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\query.py", line 1045, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\sql\compiler.py", line 1054, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\backends\mysql\base.py", line 110, in execute
    return self.cursor.execute(query, args)
  File "C:\Users\rHotD\AppData\Roaming\Python\Python35\site-packages\pymysql\cursors.py", line 166, in execute
    result = self._query(query)
  File "C:\Users\rHotD\AppData\Roaming\Python\Python35\site-packages\pymysql\cursors.py", line 322, in _query
    conn.query(q)
  File "C:\Users\rHotD\AppData\Roaming\Python\Python35\site-packages\pymysql\connections.py", line 856, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "C:\Users\rHotD\AppData\Roaming\Python\Python35\site-packages\pymysql\connections.py", line 1057, in _read_query_result
    result.read()
  File "C:\Users\rHotD\AppData\Roaming\Python\Python35\site-packages\pymysql\connections.py", line 1340, in read
    first_packet = self.connection._read_packet()
  File "C:\Users\rHotD\AppData\Roaming\Python\Python35\site-packages\pymysql\connections.py", line 1014, in _read_packet
    packet.check_error()
  File "C:\Users\rHotD\AppData\Roaming\Python\Python35\site-packages\pymysql\connections.py", line 393, in check_error
    err.raise_mysql_exception(self._data)
  File "C:\Users\rHotD\AppData\Roaming\Python\Python35\site-packages\pymysql\err.py", line 107, in raise_mysql_exception
    raise errorclass(errno, errval)
django.db.utils.DataError: (1406, "Data too long for column 'gender' at row 1")

经检查,此字段最大长度设置有误

这里写图片描述

修改一下:

这里写图片描述

这回好了

这里写图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
这个错误通常是由于数据库中的字段长度限制导致的。当你尝试将一个值保存到数据库中,但该值的长度超过了字段定义的长度时,就会出现这个错误。 要解决这个问题,你可以考虑以下几个步骤: 1. 检查你的数据库表模式中 `concentration` 字段的定义。确保它的长度足够大以容纳你要保存的值。你可以使用 `FloatField` 或者 `DecimalField` 来存储浮点数类型的数据。 例如,在模型类中使用 `DecimalField` 定义 `concentration` 字段: ```python from django.db import models class MyModel(models.Model): concentration = models.DecimalField(max_digits=10, decimal_places=2) ``` 这个示例中,`max_digits` 表示数字的总位数,`decimal_places` 表示小数点后的位数。你可以根据需要调整这些值。 2. 如果你已经在数据库中创建了表,但是字段长度不够,你需要考虑修改数据库表结构以扩展字段的长度。你可以使用 Django 的数据库迁移工具来实现这一点。运行以下命令: ```bash python manage.py makemigrations python manage.py migrate ``` 这将生成新的数据库迁移文件并将其应用于数据库,更新字段的长度。 3. 如果你不希望修改数据库表结构,而是想确保保存的值不会超过字段的长度限制,可以在保存数据之前验证字段的长度。在视图或表单中添加验证逻辑来确保值的长度合法。 希望这些步骤可以帮助你解决这个问题。如果问题仍然存在,请提供更多的细节和相关的代码,以便我能够更好地帮助你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值