Skip to content

fix: ensure AddIndex, AddConstraint, RemoveIndex and RemoveConstraint are in correct order#2118

Open
tsifrer wants to merge 3 commits intotortoise:developfrom
tsifrer:fix-index-order
Open

fix: ensure AddIndex, AddConstraint, RemoveIndex and RemoveConstraint are in correct order#2118
tsifrer wants to merge 3 commits intotortoise:developfrom
tsifrer:fix-index-order

Conversation

@tsifrer
Copy link
Contributor

@tsifrer tsifrer commented Feb 25, 2026

Description

Ensures migration generator now correctly orders AddIndex , AddConstraint, RemoveIndex, RemoveConstraint when adding/removing a new indexed field or constraint field to a model.

Motivation and Context

When adding a new field on an existing model, and using that field in as an index or a constraint, the migration creates operations in the wrong order.
Going from this model:

class OldWidget(models.Model):
    id = fields.IntField(pk=True)

to this model:

class OldWidget(models.Model):
    id = fields.IntField(pk=True)
    value = fields.IntField()
    
    class Meta:
        indexes = [Index(fields=["value"])]

would generate a migration file with operations in order: [AddIndex, AddField] causing migration to fail as it can't add an index to a non existing field.
This PR fixes this order.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 25, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing tsifrer:fix-index-order (18400aa) with develop (314273c)

Open in CodSpeed

@tsifrer tsifrer changed the title fix: ensure AddField is before AddIndex or AddConstraint in migrations fix: ensure AddIndex, AddConstraint, RemoveIndex and RemoveConstraint are in correct order Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant