Opened 20 months ago
Closed 5 months ago
#35305 closed Cleanup/optimization (fixed)
No-op rename of field with `db_column` drops and recreates constraints/indexes.
| Reported by: | Jacob Walls | Owned by: | Jacob Walls |
|---|---|---|---|
| Component: | Migrations | Version: | 4.2 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | 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 (10)
comment:1 by , 20 months ago
| Summary: | No-op rename of field with `db_column` drops and recreates constraints → No-op rename of field with `db_column` drops and recreates constraints/indexes. |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
follow-up: 3 comment:2 by , 20 months 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.
comment:3 by , 15 months ago
Replying to Giannis Terzopoulos:
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.
I commented on #35308 to see if issue is still there or some one is working on it
Maybe i can give it a try if no one else is trying , the old PR was closed due to solution not being right ,so lets see
comment:5 by , 13 months ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:6 by , 13 months ago
comment:7 by , 5 months ago
| Owner: | changed from to |
|---|
comment:9 by , 5 months ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
Tentatively accepted.