[Django] #35305: No-op rename of field with `db_column` drops and recreates constraints

3 views
Skip to first unread message

Django

unread,
Mar 15, 2024, 10:32:48 AM3/15/24
to django-...@googlegroups.com
#35305: No-op rename of field with `db_column` drops and recreates constraints
------------------------------------------------+------------------------
Reporter: Jacob Walls | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Migrations | Version: 4.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/35305>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Reply all
Reply to author
Forward
0 new messages