Opened 5 weeks ago

Closed 5 weeks ago

#35679 closed Uncategorized (duplicate)

Existing old migrations with migrations.AlterIndexTogether seem to break after Django 5.1

Reported by: Simon Willison Owned by:
Component: Migrations Version: 5.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

I may be wrong about this, but it appears as if having an existing migration in a Django project that includes something like this:

from django.db import migrations, models


class Migration(migrations.Migration):
    initial = True

    dependencies = []

    operations = [
        migrations.CreateModel(
            name="SubscriberCount",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("path", models.CharField(max_length=128)),
                ("count", models.IntegerField()),
                ("created", models.DateTimeField(auto_now_add=True)),
                ("user_agent", models.CharField(db_index=True, max_length=128)),
            ],
        ),
        migrations.AlterIndexTogether(
            name="subscribercount",
            index_together=set([("path", "user_agent", "count", "created")]),
        ),
    ]

No longer works in the latest Django (I think since 5.1) - attempts to run migrations produce errors that look like this:

  File "/Users/simon/.local/share/virtualenvs/simonwillisonblog-izSE3ZQu/lib/python3.10/site-packages/django/db/models/base.py", line 371, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/simon/.local/share/virtualenvs/simonwillisonblog-izSE3ZQu/lib/python3.10/site-packages/django/db/models/options.py", line 220, in contribute_to_class
    raise TypeError(
TypeError: 'class Meta' got invalid attribute(s): index_together

If I'm right, this is happening because index_together was deprecated and removed from Django. The problem is that this means projects with existing migrations can no longer run.

Potential solutions here:

  1. Make sure index_together in older migrations does not cause an error.
  2. Provide documentation describing how to repair projects which are affected by this bug. I hacked around it with some rough commenting out of code in old migrations but I'm not at all confident it was the right thing to do: https://github.com/simonw/simonwillisonblog/pull/480/commits/f0aa2d69ddeca2acf7badc9ec68dfb7c5b0086ab
  3. A combination of the above - make sure it works, but also document what people should do in an upgrade scenario.

Change History (2)

comment:1 by Simon Willison, 5 weeks ago

Here's my own PR where I figured out a solution to this: https://github.com/simonw/simonwillisonblog/pull/480

comment:2 by Sarah Boyce, 5 weeks ago

Resolution: duplicate
Status: newclosed

I think this is a duplicate of #34856

The 4.2 release notes say the following:

Next, consider squashing migrations to remove index_together from historical migrations.

Note: See TracTickets for help on using tickets.
Back to Top