【问题标题】:Values larger than 1/3 of a buffer page cannot be indexed Django error大于缓冲区页面 1/3 的值无法被索引 Django 错误
【发布时间】:2017-06-17 16:40:15
【问题描述】:

我正在使用 Heroku 来部署我的 Django 应用程序,我正在尝试使用以下模型将很长的数据放入表中(将数据添加到 content:

models.py:

class ContentModel(models.Model):
    ref_id = models.CharField(primary_key=True, max_length=120, unique=True)
    content = models.TextField()  #<-------
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)

    def __str__(self):
        return self.content

    class Meta:
        unique_together = ("content", "ref_id")

但我收到以下错误:

  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
psycopg2.OperationalError: index row size 3496 exceeds maximum 2712 for index "editor_contentmodel_content_2192f49c_uniq"
HINT:  Values larger than 1/3 of a buffer page cannot be indexed.
Consider a function index of an MD5 hash of the value, or use full text indexing.

我不确定这里的索引是什么意思,如果是用于搜索,我认为我不需要它,我想做的就是从表中检索数据。

它还要求我提供 MD5 哈希,所以在将内容添加到数据库之前我应该​​使用hashlib 吗?问题是我将 JSON 添加到 content

【问题讨论】:

    标签: python django postgresql heroku


    【解决方案1】:

    您面临的问题与唯一共同索引有关。考虑到您拥有的数据对于索引行大小来说太大了,您应该使用进入索引 MD5 的函数来缩小数据是足够好的选择,因为它仍然保持数据的某种唯一性。

    您应该删除旧索引并创建类似的内容

    CREATE UNiQUE INDEX sowowindex ON mytable (md5(content), ref_id);
    

    【讨论】:

    • 这是 Postgre 数据库特定的东西,据我所知 Django 并不直接支持它
    • 我已经创建了唯一字段,我可以更改表格吗?
    • 我宁愿放弃它并重新创建一个新的
    • 我没有一起使用 unique 但有同样的错误
    • @PavloKovalov 不需要是唯一索引来解决它相同或相似的问题。您正在创建哪种字段的哪种索引(发布问题而不是在 cmets 中询问),解决方案可能相同
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    • 1970-01-01
    • 2019-01-04
    • 1970-01-01
    相关资源
    最近更新 更多