Opened 6 years ago

Closed 6 years ago

#30214 closed Bug (invalid)

Indexes defined on abstract base classes are not propagated to subclasses

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

Description

It seems that if I define a model with Meta.abstract=True then any indexes I define in that model are not propgated to subclasses.

Example:

class CustomBase(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100)

    class Meta:
        abstract = True
        indexes = [
            models.Index(fields=['name'], name='name_idx')
        ]

class CustomSub(CustomBase):
    ip_address = models.GenericIPAddressField()

    class Meta:
        indexes = [
            models.Index(fields=['ip_address'], name='ip_address')
        ]

Then

>> python manage.py makemigrations
- Create model CustomSub                                           
- Create index ip_address on field(s) ip_address of model customsub

No mention of index 'name_idx' from the base class

Change History (2)

comment:1 by Simon Charette, 6 years ago

Does it happen even if you don't define any CustomSub.Meta.indexes?

comment:2 by Tim Graham, 6 years ago

Resolution: invalid
Status: newclosed
Type: UncategorizedBug

Normal Python inheritance rules apply to Meta Use:

indexes = CustomBase.Meta.indexes + [
    models.Index(fields=['ip_address'], name='ip_address')
]
Note: See TracTickets for help on using tickets.
Back to Top