Opened 8 months ago
Last modified 6 weeks ago
#35305 assigned Cleanup/optimization
No-op rename of field with `db_column` drops and recreates constraints/indexes.
Reported by: | Jacob Walls | Owned by: | Ahmed Ibrahim |
---|---|---|---|
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 (6)
comment:1 by , 8 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 , 8 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 , 3 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 , 6 weeks ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Tentatively accepted.