大于缓冲区页面 1/3 的值无法索引 Django 错误

Aks*_*hay 5 python django postgresql heroku

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

模型.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")
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

  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.
Run Code Online (Sandbox Code Playgroud)

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

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

ikl*_*nac 3

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

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

CREATE UNiQUE INDEX sowowindex ON mytable (md5(content), ref_id);
Run Code Online (Sandbox Code Playgroud)