when swapping the `primary_key=True` attribute between two model fields,
`migrations.AlterField` seems to generate a redundant UNIQUE constraint
involving the new PK field.
To reproduce:
Prerequisites:
PostgreSQL 11.12
Python 3.8
Django main@7d909b2282c91c5bc5204dd160412e90fa86d624
Before state:
{{{
class Pony(models.Model):
name = models.CharField(max_length=32, primary_key=True)
nickname = models.CharField(max_length=32)
}}}
sqlmigrate:
{{{
BEGIN;
--
-- Create model Pony
--
CREATE TABLE "app_pony" ("name" varchar(32) NOT NULL PRIMARY KEY,
"nickname" varchar(32) NOT NULL);
CREATE INDEX "app_pony_name_204da2d2_like" ON "app_pony" ("name"
varchar_pattern_ops);
COMMIT;
}}}
After state:
{{{
class Pony(models.Model):
name = models.CharField(max_length=32)
nickname = models.CharField(max_length=32, primary_key=True)
}}}
sqlmigrate:
{{{
BEGIN;
--
-- Alter field name on pony
--
DROP INDEX IF EXISTS "app_pony_name_204da2d2_like";
--
-- Alter field nickname on pony
--
ALTER TABLE "app_pony" ADD CONSTRAINT "app_pony_nickname_1b2a0f82_uniq"
UNIQUE ("nickname");
ALTER TABLE "app_pony" ADD CONSTRAINT "app_pony_nickname_1b2a0f82_pk"
PRIMARY KEY ("nickname");
CREATE INDEX "app_pony_nickname_1b2a0f82_like" ON "app_pony" ("nickname"
varchar_pattern_ops);
COMMIT;
}}}
The `app_pony_nickname_1b2a0f82_uniq` constraint looks reduntant to me,
since the `nickname` field is going to become PK anyways.
Please let me know if I'm missing something or if I can be of further
help.
Thanks,
Fabio
--
Ticket URL: <https://code.djangoproject.com/ticket/33125>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted
Comment:
Thanks for the report. We can probably add the same behavior as in #29496
(see 6dd4edb1b4f5441c5f543e29395039839c50d10b) on Oracle to other
backends.
--
Ticket URL: <https://code.djangoproject.com/ticket/33125#comment:1>
* owner: nobody => Jordan Bae
* status: new => assigned
Comment:
I will make PR for this.
--
Ticket URL: <https://code.djangoproject.com/ticket/33125#comment:2>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/33125#comment:3>
* needs_better_patch: 0 => 1
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/33125#comment:4>
* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
Comment:
Thank you for reviewing! I updated the code :)
--
Ticket URL: <https://code.djangoproject.com/ticket/33125#comment:5>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/33125#comment:6>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"f5a3243911a5c42775cd838a3da1d0f3ac4a74ee" f5a32439]:
{{{
#!CommitTicketReference repository=""
revision="f5a3243911a5c42775cd838a3da1d0f3ac4a74ee"
Fixed #33125 -- Avoided redundant unique constraint when converting a non-
unique field to primary key on MySQL and PostgreSQL.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33125#comment:7>