ashes999
ashes999

Reputation: 1324

Error adding GinIndex to model in Django with Postgres

I am trying to add GinIndex to my model however, I am getting an error when migrating:

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.

This is my model:

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')
        ]

I was wondering what is causing this issue I cannot find how to resolve this issue.

Thanks in advance!

Upvotes: 3

Views: 915

Answers (1)

Oscar Chen
Oscar Chen

Reputation: 636

Inside the migration file created by Django for adding the GinIndex, add the following:

from django.contrib.postgres.operations import BtreeGinExtension

and then add this to the inside of the operations list, ie:

operations = [
    BtreeGinExtension()
    ...
]

Run this migration again and it should go through.

Upvotes: 4

Related Questions