使用 Postgres 将 GinIndex 添加到 Django 中的模型时出错



我正在尝试将 GinIndex 添加到我的模型中,但是,迁移时出现错误:

django.db.utils.ProgrammingError: data type character varying has no default operator class for access method "gin"
HINT:  You must specify an operator class for the index or define a default operator class for the data type.

这是我的模型:

class Post(models.Model):
title = models.CharField(max_length=100)
guid_url = models.CharField(max_length=255,unique=True, null=True)
content = models.TextField(validators=[MaxLengthValidator(1200)])
author = models.ForeignKey(Profile, on_delete=models.CASCADE)
date_posted = models.DateTimeField(auto_now_add=True)
last_edited= models.DateTimeField(auto_now=True)
tags = TaggableManager(help_text="Tags", blank=True)
likes= models.ManyToManyField(Profile, blank=True, related_name='post_likes')

class Meta:
indexes = [
GinIndex(fields=['title','content'],name='search_idx_post')
]

我想知道是什么导致了这个问题,我找不到如何解决这个问题。

提前感谢!

在 Django 为添加 GinIndex 而创建的迁移文件中,添加以下内容:

from django.contrib.postgres.operations import BtreeGinExtension

然后将其添加到operations列表的内部,即:

operations = [
BtreeGinExtension()
...
]

再次运行此迁移,它应该会完成。

最新更新