python - Django psycopg2.errors.StringDataRightTruncation:对于类型字符变化的值太长(200)
问题描述
运行 django 应用程序时遇到上述错误。究竟需要改变什么?comment_body = models.TextField() 方面很可能是罪魁祸首,因为它存储了可以不同长度的 reddit 评论。当我做一个 git clone 并在我的本地运行它时,奇怪的是它可以工作。
模型.py
from django.db import models
# Create your models here.
class Subreddit(models.Model):
# Field for storing the name of a subreddit
subreddit_name = models.CharField(max_length=200, unique=True)
# Field for storing the time the model object was last saved
last_updated = models.DateTimeField(auto_now=True)
class Submission(models.Model):
subreddit = models.ForeignKey(Subreddit, on_delete=models.CASCADE)
# The Reddit submission id of the object
submission_id = models.CharField(max_length=200, unique=True)
# Reddit Submission URL
url = models.URLField(max_length=200)
# Reddit Submission Title
title = models.CharField(max_length=200)
class SubmissionComment(models.Model):
# Parent submission object
submission = models.ForeignKey(Submission, on_delete=models.CASCADE)
# Text of the comment
comment_body = models.TextField()
class Meme(models.Model):
memeurl = models.URLField(max_length=200)
编辑:
解决方案
鉴于错误:value too long for type character varying(200)
您应该查找 amax_length
为 200 的模型字段。由于您有多个字段的 max_length 设置为 200,因此您需要确定哪个模型和字段引发错误。检查堆栈跟踪,运行调试器和/或插入一些调试print(instance.__dict__)
。找到罪魁祸首后,将该字段扩展max_length
到更大的内容或将其转换为TextField
.
推荐阅读
- javascript - 表单提交输入未运行我的 onClick 功能
- prometheus - Spring boot 2.3.4 - Kafka 指标在 /actuator/prometheus 中不可见
- python - 从维基百科页面的类别中获取更一般的类别
- python - 如何在两个向量之间生成一个随机向量?
- javascript - XMPP - 如何删除两个 jid 之间的所有消息,但仅限于一个用户?
- c# - 从移动设备将提货单推送到结账终端
- node.js - 如何在 NodeJS 中对 MySQL 数据库进行异步调用
- javascript - TypeError:req.body.forEach 不是函数
- c# - 开始x数量的线程
- flutter - Flutter:NoSuchMethodError 仅在 Android 上