首页 > 解决方案 > Django psycopg2.errors.StringDataRightTruncation:对于类型字符变化的值太长(200)

问题描述

运行 django 应用程序时遇到上述错误。究竟需要改变什么?comment_body = models.TextField() 方面很可能是罪魁祸首,因为它存储了可以不同长度的 reddit 评论。当我做一个 git clone 并在我的本地运行它时,奇怪的是它可以工作。

Heroku 日志

模型.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)

编辑:

新的错误 post char 更改为 300,并且迁移在本地和 heroku 上运行。 新错误,但错误在哪里

标签: pythonpython-3.xdjangopostgresqlheroku

解决方案


鉴于错误:value too long for type character varying(200)您应该查找 amax_length为 200 的模型字段。由于您有多个字段的 max_length 设置为 200,因此您需要确定哪个模型和字段引发错误。检查堆栈跟踪,运行调试器和/或插入一些调试print(instance.__dict__)。找到罪魁祸首后,将该字段扩展max_length到更大的内容或将其转换为TextField.


推荐阅读