Opened 3 months ago

Closed 3 months ago

Last modified 3 months ago

#35274 closed Bug (wontfix)

Avoid creating index using db_index=False for a CharField which is a primary key

Reported by: ChamathAilapperuma Owned by: nobody
Component: Database layer (models, ORM) Version: 4.2
Severity: Normal Keywords: primary_key, db_index
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm trying to remove all the unused indexes from my db and figured that there are some primary key indexes created by Django. Following is an example for a model that I need to remove those indexes from.

class TeamName(models.Model):
    team_name = models.CharField(max_length=64, primary_key=True)

I tried adding db_index=False to above team_name field expecting it to avoid creating the index for the primary key. But it does not create a migration for this change. Thus it does not remove the corresponding index with name ending "_like". All of my models having CharField as primary key behaves the same.

Change History (1)

comment:1 by David Sanders, 3 months ago

Resolution: wontfix
Status: newclosed

Thanks, though not sure what you're trying to achieve. Specifying "primary key" on a field implies that unique indexes will be created, otherwise fks won't be able to target it. If your goal is to simply remove the "_like" index then you can do this by wrapping the auto-generated migration in a SeparateDatabaseAndState migration to customise the way it creates the table. (You could also add a manual migration to simply drop the index too).

Honestly though it sounds like you could benefit from starting a thread on the Django forum to discuss your problem, others may have additional ways to help you: https://www.djangoproject.com/community/

Last edited 3 months ago by David Sanders (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top