Opened 4 years ago

Closed 4 years ago

#32393 closed Bug (invalid)

Indexes of only the first abstract base class are included in the model

Reported by: Dmitry Ulupov Owned by: nobody
Component: Migrations Version: 3.1
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

If model has multiple base abstract models, their Meta.indexes declarations are treated differently.

Only indexes from the first class are taken into account.
Indexes of all other base classes are ignored.

Consider this code:

class ParentA(models.Model):
    field_a = models.CharField(max_length=10)

    class Meta:
        abstract = True


class ParentB(models.Model):
    field_b = models.CharField(max_length=10)

    class Meta:
        abstract = True
        indexes = [
            models.Index(
                fields=["field_b"],
                name="ix_%(app_label)s_%(class)s",
            ),
        ]


class ChildOf_A_B(ParentA, ParentB):
    pass


class ChildOf_B_A(ParentB, ParentA):
    pass

makemigrations in this case produces this:

./manage.py makemigrations app

Migrations for 'app':
  project/app/migrations/0001_initial.py
    - Create model ChildOf_A_B
    - Create model ChildOf_B_A
    - Create index ix_app_childof_b_a on field(s) field_b of model childof_b_a

So index on ChildOf_B_A is created, but on ChildOf_A_B is not.
And only difference is the order of declaration of the base classes.

Change History (2)

comment:1 by Dmitry Ulupov, 4 years ago

Just finished re-reading the "multiple inheritance" section and this is an expected behaviour.
First class indeed clobbers all others and indexes declaration is lost.
Solution is not to have such convoluted structure as I have.

comment:2 by Dmitry Ulupov, 4 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top