Opened 7 weeks ago

Last modified 6 weeks ago

#35305 new Cleanup/optimization

No-op rename of field with `db_column` drops and recreates constraints/indexes.

Reported by: Jacob Walls Owned by: nobody
Component: Migrations Version: 4.2
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

With this model:

class MyModel(models.Model):
    foo = models.BooleanField(db_column="foobar")

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=["foo"],
                name="unique_foo",
            ),
        ]

Suppose I wish to improve the name of the field so it agrees with the database column name:

class MyModel(models.Model):
    foobar = models.BooleanField(db_column="foobar")

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=["foobar"],
                name="unique_foo",
            ),
        ]

That change generates a migration with this SQL, assuming you answer "was the field ... renamed..." with Yes:

BEGIN;
--
-- Remove constraint unique_foo from model mymodel
--
ALTER TABLE "models_mymodel" DROP CONSTRAINT "unique_foo";
--
-- Rename field foo on mymodel to foobar
--
-- (no-op)
--
-- Create constraint unique_foo on model mymodel
--
ALTER TABLE "models_mymodel" ADD CONSTRAINT "unique_foo" UNIQUE ("foobar");
COMMIT;

Suggesting that we compare on db_column if present to allow this kind of change to be a no-op with respect to constraints.

Change History (2)

comment:1 by Mariusz Felisiak, 7 weeks ago

Summary: No-op rename of field with `db_column` drops and recreates constraintsNo-op rename of field with `db_column` drops and recreates constraints/indexes.
Triage Stage: UnreviewedAccepted

Tentatively accepted.

comment:2 by Giannis Terzopoulos, 6 weeks ago

I had a brief look at this and it seems related to #35038 and the suggestion to add a new AlterConstraint operation. So maybe it's best to have that operation in place before someone works on this ticket.

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