﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
37519	Removing unique_together in the same migration as CreateModel drops a pending index, or crashes with KeyError: 'columns'	Mariano Ignacio Baragiola	Mariano Ignacio Baragiola	"Since cd385e6b8c16b51f68c1f220ff09a4cfd679af0c (#31317, backported to 6.1 as 622aabce56), `BaseDatabaseSchemaEditor._delete_composed_index()` first looks in `deferred_sql` for the unique_together statement. It removes the first `Statement` on the table that references all the columns and whose `parts[""columns""]` match. It does not check that this statement is the unique_together one. An index or foreign key on the same column is also a deferred statement on that column, so:

 * a pending `db_index` (or foreign key) index is removed from `deferred_sql` and never created, and the real unique constraint stays in the database;
 * a pending foreign key constraint has a `column` part, not `columns`, so `deferred.parts[""columns""]` raises `KeyError: 'columns'` on PostgreSQL.

This needs CreateModel, AlterUniqueTogether (add) and AlterUniqueTogether (remove) in one migration, for example a hand-written or squashed migration (a RunPython between them stops the optimizer from merging them).

Reproduction (`tests/migrations/test_operations.py` style):

{{{#!python
self.apply_operations(
    ""test_aluntodi"",
    ProjectState(),
    operations=[
        migrations.CreateModel(
            ""Pony"",
            fields=[
                (""id"", models.AutoField(primary_key=True)),
                (""weight"", models.IntegerField(db_index=True)),
            ],
        ),
        migrations.AlterUniqueTogether(name=""Pony"", unique_together={(""weight"",)}),
        migrations.AlterUniqueTogether(name=""Pony"", unique_together=set()),
    ],
)
}}}

Actual (SQLite and PostgreSQL): there is no index on `weight`, and the unique constraint on `weight` is still there.

Expected: a plain index on `weight` and no unique constraint, as in Django 6.0.

With `stable = models.ForeignKey(""test_aluntodfk.Stable"", models.CASCADE)` instead of `weight`, PostgreSQL raises:

{{{
  File ""django/db/backends/base/schema.py"", line 679, in _delete_composed_index
    and deferred.parts[""columns""].columns == columns
        ~~~~~~~~~~~~~~^^^^^^^^^^^
KeyError: 'columns'
}}}

On SQLite the foreign key index is dropped and the unique constraint stays, as above.

Versions: main and 6.1.0 to 6.1.3. 6.0.x works.

Regression in cd385e6b8c16b51f68c1f220ff09a4cfd679af0c. A reviewer on PR 20929 suggested guarding the `columns` lookup; that guard was not merged, and it would fix only the crash, not the dropped index.

Suggested fix: only remove the statement that `create_model()` deferred for unique_together, i.e. one whose `name` part is the `_uniq` `IndexName` that `_unique_constraint_name()` builds. A patch with tests for both cases follows.

AI disclosure: I found and reproduced this with Claude Code (Anthropic)."	Bug	assigned	Migrations	6.1	Release blocker				Unreviewed	0	0	0	0	0	0
