4

I am using a CharField in django to store a primary key. I have defined this as:

    id = models.CharField(
        max_length=64, default=create_id, primary_key=True, null=False, editable=False
    )

This results in my table having the following two indexes:

 CREATE UNIQUE INDEX api_participant_pkey ON public.api_participant USING btree (id) 
 CREATE INDEX api_participant_id_cb12634b_like ON public.api_participant USING btree (id varchar_pattern_ops)

The text in the id is meaningless, I am always going to query that field through an exact match, never using LIKE.

Is there an easy way to remove the _like index?

I've seen How to remove index varchar_pattern_ops in a django (1.8) migration? on how to remove the index when it is a field being indexed, but I am not sure how to do this for a primary or foreign key.

I'm using postgresql and Django==2.2.13 and this is a live database I cannot just recreate.

Mario Corchero
  • 5,257
  • 5
  • 33
  • 59
  • It is unclear if question is how to remove already created index or how to fix migration for this not happening in a future considering you have stated it is live database – iklinac Dec 27 '20 at 18:37
  • This answer in the linked question seems to provide a "hacky" solution? https://stackoverflow.com/a/65177940/548562 – Iain Shelvington Dec 27 '20 at 18:41
  • It is to create a new migration to drop those indexes. Thanks @IainShelvington I'm gonna try that one, I totally missed it, I think I can adapt it for that usecase. I'll post an answer if I manage to get that through. – Mario Corchero Dec 28 '20 at 10:52

0 Answers0