Opened 2 years ago

Closed 2 years ago

#33947 closed Uncategorized (invalid)

Adding db_index to a field inherited from an Abstract class does not propagate the change to the models subclassing it

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

Description (last modified by awiebe)

I have a bunch of Models of the form

  class AbstractOwnershipModel(models.Model):
     owner_user= models.ForeignKey('auth.User')
     class Meta:
      abstract = True
 class AnOwnedModel(AbstractOwnershipModel):
   #...

Which I tried to change to

class AbstractOwnershipModel(models.Model):
   owner_user= models.ForeignKey('auth.User',db_index=True)
   class Meta:
    abstract = True

But makemigrations reports that there are no changes even though I would expect a bunch of indexes to be created.

Change History (2)

comment:1 by awiebe, 2 years ago

Description: modified (diff)

comment:2 by Alex Morega, 2 years ago

Resolution: invalid
Status: newclosed

But makemigrations reports that there are no changes even though I would expect a bunch of indexes to be created.

That's because the index is already created by default:

A database index is automatically created on the ForeignKey. You can disable this by setting db_index to False. You may want to avoid the overhead of an index if you are creating a foreign key for consistency rather than joins, or if you will be creating an alternative index like a partial or multiple column index.

You can check by connecting to the database using ./manage.py dbshell and looking at the database schema.

Note: See TracTickets for help on using tickets.
Back to Top