[Django] #36357: inspectdb creates a redundant unique_together for composite primary keys

10 views
Skip to first unread message

Django

unread,
Apr 28, 2025, 3:19:15 AMApr 28
to django-...@googlegroups.com
#36357: inspectdb creates a redundant unique_together for composite primary keys
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Type:
| Cleanup/optimization
Status: new | Component: Database
| layer (models, ORM)
Version: 5.2 | Severity: Normal
Keywords: CompositePrimaryKey | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
With the following table:
{{{#!sql
CREATE TABLE example (
a integer,
b integer,
c integer,
PRIMARY KEY (a, c)
);
}}}

Running `inspectdb` produces the following model definition:
{{{#!py
class Example(models.Model):
pk = models.CompositePrimaryKey('a', 'c')
a = models.IntegerField()
b = models.IntegerField(blank=True, null=True)
c = models.IntegerField()

class Meta:
managed = False
db_table = 'example'
unique_together = (('a', 'c'),)
}}}

I'm pretty sure the last `unique_together = (('a', 'c'),)` is redundant
since the `pk = models.CompositePrimaryKey('a', 'c')` already implies
uniqueness, no?
--
Ticket URL: <https://code.djangoproject.com/ticket/36357>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 28, 2025, 4:10:47 AMApr 28
to django-...@googlegroups.com
#36357: inspectdb creates a redundant unique_together for composite primary keys
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: Baptiste
Type: | Mispelon
Cleanup/optimization | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: CompositePrimaryKey | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Baptiste Mispelon):

* has_patch: 0 => 1
* owner: (none) => Baptiste Mispelon
* status: new => assigned

Comment:

Here's a draft PR with a test and a fix:
https://github.com/django/django/pull/19430

I'll add release notes if the ticket is accepted.
--
Ticket URL: <https://code.djangoproject.com/ticket/36357#comment:1>

Django

unread,
Apr 28, 2025, 9:33:34 AMApr 28
to django-...@googlegroups.com
#36357: inspectdb creates a redundant unique_together for composite primary keys
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: Baptiste
| Mispelon
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: CompositePrimaryKey | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* severity: Normal => Release blocker
* stage: Unreviewed => Accepted
* type: Cleanup/optimization => Bug

Comment:

Thanks for the report Baptiste.

Marked as a bug in a newly released feature.
--
Ticket URL: <https://code.djangoproject.com/ticket/36357#comment:2>

Django

unread,
Apr 28, 2025, 11:51:03 AMApr 28
to django-...@googlegroups.com
#36357: inspectdb creates a redundant unique_together for composite primary keys
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: Baptiste
| Mispelon
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: CompositePrimaryKey | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* needs_docs: 0 => 1

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

Django

unread,
Apr 30, 2025, 10:16:07 AMApr 30
to django-...@googlegroups.com
#36357: inspectdb creates a redundant unique_together for composite primary keys
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: Baptiste
| Mispelon
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: CompositePrimaryKey | 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 Natalia Bidart):

* needs_docs: 1 => 0
* stage: Accepted => Ready for checkin

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

Django

unread,
Apr 30, 2025, 10:55:35 AMApr 30
to django-...@googlegroups.com
#36357: inspectdb creates a redundant unique_together for composite primary keys
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: Baptiste
| Mispelon
Type: Bug | Status: closed
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: CompositePrimaryKey | 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 nessita <124304+nessita@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"66f9eb0ff1e7147406318c5ba609729678e4e6f6" 66f9eb0f]:
{{{#!CommitTicketReference repository=""
revision="66f9eb0ff1e7147406318c5ba609729678e4e6f6"
Fixed #36357 -- Skipped unique_together in inspectdb output for composite
primary keys.

Thanks to Baptiste Mispelon for the report and quick fix, and to Simon
Charette and Jacob Walls for the reviews.

Co-authored-by: Natalia <124304+...@users.noreply.github.com>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36357#comment:5>

Django

unread,
Apr 30, 2025, 10:56:25 AMApr 30
to django-...@googlegroups.com
#36357: inspectdb creates a redundant unique_together for composite primary keys
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: Baptiste
| Mispelon
Type: Bug | Status: closed
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: CompositePrimaryKey | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Natalia <124304+nessita@…>):

In [changeset:"1367a197dda2027e4fcf33e3053244fdcbda9d8c" 1367a197]:
{{{#!CommitTicketReference repository=""
revision="1367a197dda2027e4fcf33e3053244fdcbda9d8c"
[5.2.x] Fixed #36357 -- Skipped unique_together in inspectdb output for
composite primary keys.

Thanks to Baptiste Mispelon for the report and quick fix, and to Simon
Charette and Jacob Walls for the reviews.

Co-authored-by: Natalia <124304+...@users.noreply.github.com>

Backport of 66f9eb0ff1e7147406318c5ba609729678e4e6f6 from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36357#comment:6>
Reply all
Reply to author
Forward
0 new messages