[Django] #33125: Changing model field to become PK creates redundant UNIQUE constraint

3 views
Skip to first unread message

Django

unread,
Sep 21, 2021, 9:08:48 AM9/21/21
to django-...@googlegroups.com
#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.

Django

unread,
Sep 22, 2021, 2:49:24 AM9/22/21
to django-...@googlegroups.com
#33125: Changing model field to become PK creates redundant UNIQUE constraint
--------------------------------------+------------------------------------

Reporter: Fabio Sangiovanni | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Mariusz Felisiak):

* 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>

Django

unread,
Sep 24, 2021, 7:17:33 PM9/24/21
to django-...@googlegroups.com
#33125: Changing model field to become PK creates redundant UNIQUE constraint
-------------------------------------+-------------------------------------
Reporter: Fabio Sangiovanni | Owner: Jordan
Type: | Bae
Cleanup/optimization | Status: assigned
Component: Migrations | Version: dev

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jordan Bae):

* owner: nobody => Jordan Bae
* status: new => assigned


Comment:

I will make PR for this.

--
Ticket URL: <https://code.djangoproject.com/ticket/33125#comment:2>

Django

unread,
Sep 25, 2021, 3:37:24 AM9/25/21
to django-...@googlegroups.com
#33125: Changing model field to become PK creates redundant UNIQUE constraint
-------------------------------------+-------------------------------------
Reporter: Fabio Sangiovanni | Owner: Jordan
Type: | Bae
Cleanup/optimization | Status: assigned
Component: Migrations | Version: dev

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jordan Bae):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/33125#comment:3>

Django

unread,
Sep 27, 2021, 5:59:19 AM9/27/21
to django-...@googlegroups.com
#33125: Changing model field to become PK creates redundant UNIQUE constraint
-------------------------------------+-------------------------------------
Reporter: Fabio Sangiovanni | Owner: Jordan
Type: | Bae
Cleanup/optimization | Status: assigned
Component: Migrations | Version: dev

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1
* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/33125#comment:4>

Django

unread,
Sep 27, 2021, 8:23:21 AM9/27/21
to django-...@googlegroups.com
#33125: Changing model field to become PK creates redundant UNIQUE constraint
-------------------------------------+-------------------------------------
Reporter: Fabio Sangiovanni | Owner: Jordan
Type: | Bae
Cleanup/optimization | Status: assigned
Component: Migrations | Version: dev

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jordan Bae):

* 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>

Django

unread,
Sep 28, 2021, 12:23:08 AM9/28/21
to django-...@googlegroups.com
#33125: Changing model field to become PK creates redundant UNIQUE constraint
-------------------------------------+-------------------------------------
Reporter: Fabio Sangiovanni | Owner: Jordan
Type: | Bae
Cleanup/optimization | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/33125#comment:6>

Django

unread,
Sep 28, 2021, 1:51:39 AM9/28/21
to django-...@googlegroups.com
#33125: Changing model field to become PK creates redundant UNIQUE constraint
-------------------------------------+-------------------------------------
Reporter: Fabio Sangiovanni | Owner: Jordan
Type: | Bae
Cleanup/optimization | Status: closed
Component: Migrations | Version: dev
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* 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>

Reply all
Reply to author
Forward
0 new messages