#33125: Changing model field to become PK creates redundant UNIQUE constraint
---------------------------------------------+------------------------
               Reporter:  Fabio Sangiovanni  |          Owner:  nobody
                   Type:  Bug                |         Status:  new
              Component:  Migrations         |        Version:  dev
               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                  |
---------------------------------------------+------------------------
 Hi,

 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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.eca5ed7945747ebcd0c7fd2f289e5a17%40djangoproject.com.

Reply via email to