Opened 3 months ago

Closed 3 months ago

#33736 closed Bug (duplicate)

Unable to sqlmigrate on an AlterUniqueTogether migration

Reported by: Noelia Marí Salvador Owned by: nobody
Component: Migrations Version: 4.0
Severity: Normal Keywords: sqlmigrate
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Noelia Marí Salvador)

Reproduced in Django 4.0.4.

Given this model:

class TestModel(models.Model):
    name = models.CharField(max_length=200)
    age = models.IntegerField()
    colour = models.CharField(max_length=200)

    class Meta:
        unique_together = ["name", "age"]

When migrations are created and applied, a unique constraint is created on the database. Now, we want to alter the unique_together to:
unique_together = ["name", "colour"]

After running makemigrations, without applying the new migration, we are able to run sqlmigrate:

$ testproject python3 sqlmigrate core 0003
-- Alter unique_together for testmodel (1 constraint(s))
DROP INDEX "core_testmodel_name_age_530d7841_uniq";
CREATE UNIQUE INDEX "core_testmodel_name_colour_5002f5dd_uniq" ON "core_testmodel" ("name", "colour");

However, after we successfully apply migrations as below, sqlmigrate fails.

$ testproject python3 migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, core, sessions
Running migrations:
  Applying core.0003_alter_testmodel_unique_together... OK

So when running sqlmigrate on the same migration it crashes and throws a ValueError because it's looking for the prior index:

$ testproject python3 sqlmigrate core 0003
Traceback (most recent call last):
    raise ValueError(
ValueError: Found wrong number (0) of constraints for core_testmodel(name, age)

Full minimal example can be found here:

Change History (2)

comment:1 Changed 3 months ago by Noelia Marí Salvador

Description: modified (diff)

comment:2 Changed 3 months ago by Mariusz Felisiak

Resolution: duplicate
Status: newclosed

Duplicate of #31834.

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