#36632: AlterIndexTogether in historical migration drops overlapping indices
-------------------------------------+-------------------------------------
Reporter: Michael Herrmann | Owner: nzioker
Type: Bug | Status: assigned
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: index_together | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by nzioker):
* has_patch: 0 => 1
Comment:
Hello everyone,
I did my analysis on the ticket and found that the issue as in the
alter_index_method,
Root Cause
The problem occurred in django/db/backends/base/schema.py in two places:
alter_index_together method: Used set difference operations that only
created new indexes, not all target indexes
_model_indexes_sql method: Didn't include index_together indexes during
SQLite table remakes
The Fix
I made two minimal, targeted changes:
1. Fixed index creation logic in alter_index_together:
python
# Before the fix:
for field_names in news.difference(olds):
# After the fix:
for field_names in news:
2. Added historical index support in _model_indexes_sql:
python
# Ensure index_together indexes are recreated during table remakes
if hasattr(model._meta, 'index_together') and model._meta.index_together:
for field_names in model._meta.index_together:
fields = [model._meta.get_field(field) for field in field_names]
output.append(self._create_index_sql(model, fields=fields,
suffix="_idx"))
This fix ensures that AlterIndexTogether replaces the entire index and
handles both direct operations and table remakes.
Opted not to pursue the delegation approach and instead as it was more
complex and would require creatio of unnecessary operation objects.
Besides this, the root cause of the bug was in schema.py
Patch found in:
https://github.com/django/django/pull/20013
Submitting the PR shortly.
--
Ticket URL: <
https://code.djangoproject.com/ticket/36632#comment:3>