django.db.utils.DataError: (1406, "Data too long for column 'location' at row 1")

    (cmm_owms) [root@usasvr2 a9FriendFinder]# supervisorctl restart friend_task
    friend_task: ERROR (not running)
    friend_task: ERROR (abnormal termination)
    (cmm_owms) [root@usasvr2 a9FriendFinder]# supervisorctl start friend_task
    friend_task: ERROR (abnormal termination)
    (cmm_owms) [root@usasvr2 a9FriendFinder]# tailf app/tasks/myapp.log 
    Tue, 07 Apr 2020 15:45:11 app_task.py[line:275] INFO friend: start gps&recommender task...
    ^C
    (cmm_owms) [root@usasvr2 a9FriendFinder]# tailf /var/log/a9//friend_task.log 
        result.read()
      File "/root/python/venv/cmm_owms/lib/python3.6/site-packages/pymysql/connections.py", line 1075, in read
        first_packet = self.connection._read_packet()
      File "/root/python/venv/cmm_owms/lib/python3.6/site-packages/pymysql/connections.py", line 684, in _read_packet
        packet.check_error()
      File "/root/python/venv/cmm_owms/lib/python3.6/site-packages/pymysql/protocol.py", line 220, in check_error
        err.raise_mysql_exception(self._data)
      File "/root/python/venv/cmm_owms/lib/python3.6/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
        raise errorclass(errno, errval)
    django.db.utils.DataError: (1406, "Data too long for column 'location' at row 1")
    ^C
    (cmm_owms) [root@usasvr2 a9FriendFinder]# 
    问题已经打印得比较清楚了,就是mysql db的字段:location不够长。要写入的city字符串长度超过了db定义的长度。
    用下面的方法来解决,加断言,加try catch()语言捕获异常。
            try:
            # we save the data to mysql as gps-log-record for check
            log_item = LogUserLocation()
            log_item.user_id = user_id
            log_item.gps_lat = gps_lat
            log_item.gps_lon = gps_lon
            assert (len(str(live_data['city'])) < 800)
            log_item.location = live_data['city']
            log_item.save()
        except Exception as e:
            logging.error("e4=" + repr(e))
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这个错误通常是由于数据库中的字段长度限制导致的。当你尝试将一个值保存到数据库中,但该值的长度超过了字段定义的长度时,就会出现这个错误。 要解决这个问题,你可以考虑以下几个步骤: 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. 如果你不希望修改数据库表结构,而是想确保保存的值不会超过字段的长度限制,可以在保存数据之前验证字段的长度。在视图或表单中添加验证逻辑来确保值的长度合法。 希望这些步骤可以帮助你解决这个问题。如果问题仍然存在,请提供更多的细节和相关的代码,以便我能够更好地帮助你。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

草根大哥

进军大神程序员路上,谢谢支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值