#34525: index_together warning after migration to new style
-------------------------------------+-------------------------------------
Reporter: Mateusz Legięcki | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: index_together, | Triage Stage: Accepted
warning |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Mariusz Felisiak):
If squashing migrations is not an option for you, then you have to
manually adjust historical migrations. I'd do this in the following steps:
1. Follow the guidance in the
[https://docs.djangoproject.com/en/4.2/releases/4.2/#index-together-
option-is-deprecated-in-favor-of-indexes release notes] to get a new
migration with `RenameIndex` operation.
2. Apply migrations.
3. Add `new_name` from `RenameIndex` operations to the `Meta.indexes`,
e.g. when you have
{{{
migrations.RenameIndex(
model_name="mymodel",
new_name="test_one_my_field_1_ea7372_idx",
old_fields=("field_1", "field_2"),
),
}}}
add `"test_one_my_field_1_ea7372_idx"` to the `Index` definition:
{{{
class MyModel(models.Model):
...
class Meta:
indexes = [
models.Index(fields=['field_1', 'field_2'],
name="test_one_my_field_1_ea7372_idx"),
]
}}}
`makemigrations` should not detect any changes.
4. Remove `options["index_together"]` from the `CreateModel()` operation
and add `options["indexes"]` instead, e.g.
{{{
operations = [
migrations.CreateModel(
name='MyModel',
fields=[
('id', models.BigAutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('field_1', models.IntegerField()),
('field_2', models.TextField()),
('field_3', models.IntegerField()),
],
options={
"indexes": [
models.Index(fields=['field_1', 'field_2'],
name="test_one_my_field_1_ea7372_idx"),
],
},
),
]
}}}
5. Remove `AlterIndexTogether()` and `RenameIndex()` operations related
with this index. `makemigrations` should not detect any changes.
Some minor adjustments may be needed but it's the general guidelines that
I will follow.
--
Ticket URL: <https://code.djangoproject.com/ticket/34525#comment:22>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070187ea377908-550ffeb8-9508-420a-a8f6-1ebb0f1c8264-000000%40eu-central-1.amazonses.com.