[Django] #36358: Tables with composite primary keys including integer columns improperly get introspected as AutoField on SQLite

10 views
Skip to first unread message

Django

unread,
Apr 28, 2025, 11:33:14 PMApr 28
to django-...@googlegroups.com
#36358: Tables with composite primary keys including integer columns improperly get
introspected as AutoField on SQLite
-------------------------------------+-------------------------------------
Reporter: Simon | Owner: Simon Charette
Charette |
Type: Bug | Status: assigned
Component: Database | Version: 5.2
layer (models, ORM) |
Severity: Release | Keywords:
blocker |
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Given a models defined like

{{{#!python
class CompositePk(models.Model):
pk = models.CompositePrimaryKey("field1", "field2")
field1 = models.IntegerField()
field2 = models.IntegerField()
}}}

it incorrectly gets introspected as

{{{#!python
class CompositePk(models.Model):
pk = models.CompositePrimaryKey("field1", "field2")
field1 = models.AutoField()
field2 = models.IntegerField()
}}}

on SQLite which was missed by #36052
(a8e4fd11efc65832e7d9f5582d3868c5c8bd8d88) when support for composite
primary introspection was added even if the pre-existing test encoded this
qwirk.

This is likely due to the fact that [https://www.sqlite.org/autoinc.html
SQLite surprisingly treats any integer field primary key as auto-
incrementing] even if not explicitly specified

> In SQLite, a column with type `INTEGER PRIMARY KEY` is an alias for the
`ROWID` (except in `WITHOUT ROWID` tables) which is always a 64-bit signed
integer.

which was known when `AutoField` introspection was added in #23748
(a35d2a4510d5beec398b1007aaa26492d6aedf97) see ticket:23748#comment:11 but
the override of `get_field_type` was not properly adjusted when composite
primary key introspection was added in #32234.

Marking as a release blocker as it's a bug in a newly released feature.
--
Ticket URL: <https://code.djangoproject.com/ticket/36358>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 29, 2025, 3:37:56 AMApr 29
to django-...@googlegroups.com
#36358: Tables with composite primary keys including integer columns improperly get
introspected as AutoField on SQLite
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | 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 Sarah Boyce):

* stage: Unreviewed => Accepted

--
Ticket URL: <https://code.djangoproject.com/ticket/36358#comment:1>

Django

unread,
Apr 29, 2025, 3:48:37 AMApr 29
to django-...@googlegroups.com
#36358: Tables with composite primary keys including integer columns improperly get
introspected as AutoField on SQLite
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | 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 Sarah Boyce):

* has_patch: 0 => 1

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

Django

unread,
Apr 30, 2025, 8:03:00 AMApr 30
to django-...@googlegroups.com
#36358: Tables with composite primary keys including integer columns improperly get
introspected as AutoField on SQLite
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | 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 Natalia Bidart):

* stage: Accepted => Ready for checkin

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

Django

unread,
Apr 30, 2025, 9:52:00 AMApr 30
to django-...@googlegroups.com
#36358: Tables with composite primary keys including integer columns improperly get
introspected as AutoField on SQLite
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Owner: Simon
| Charette
Type: Bug | Status: closed
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | 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 nessita <124304+nessita@…>):

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

Comment:

In [changeset:"07100db6f46255ec6ef70b860495f977473684d6" 07100db6]:
{{{#!CommitTicketReference repository=""
revision="07100db6f46255ec6ef70b860495f977473684d6"
Fixed #36358 -- Corrected introspection of composite primary keys on
SQLite.

Previously, any first field of a composite primary key with type
`INTEGER` was incorrectly introspected as an `AutoField` due to SQLite
treating `INTEGER PRIMARY KEY` as an alias for the `ROWID`.

This change ensures that integer fields in composite PKs are not
mistaken for auto-incrementing fields.

Thanks Jacob Walls and Sarah Boyce for the reviews.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36358#comment:4>

Django

unread,
Apr 30, 2025, 9:54:51 AMApr 30
to django-...@googlegroups.com
#36358: Tables with composite primary keys including integer columns improperly get
introspected as AutoField on SQLite
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Owner: Simon
| Charette
Type: Bug | Status: closed
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | 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
-------------------------------------+-------------------------------------
Comment (by Natalia <124304+nessita@…>):

In [changeset:"ec73fd67466e0e4841d9ecd0f217c02ce842d860" ec73fd67]:
{{{#!CommitTicketReference repository=""
revision="ec73fd67466e0e4841d9ecd0f217c02ce842d860"
[5.2.x] Fixed #36358 -- Corrected introspection of composite primary keys
on SQLite.

Previously, any first field of a composite primary key with type
`INTEGER` was incorrectly introspected as an `AutoField` due to SQLite
treating `INTEGER PRIMARY KEY` as an alias for the `ROWID`.

This change ensures that integer fields in composite PKs are not
mistaken for auto-incrementing fields.

Thanks Jacob Walls and Sarah Boyce for the reviews.

Backport of 07100db6f46255ec6ef70b860495f977473684d6 from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36358#comment:5>
Reply all
Reply to author
Forward
0 new messages