[Django] #23748: inspectdb converts auto increment primary keys into primary key models.IntegerField() rather than primary key models.AutoField()

38 views
Skip to first unread message

Django

unread,
Nov 1, 2014, 8:01:14 PM11/1/14
to django-...@googlegroups.com
#23748: inspectdb converts auto increment primary keys into primary key
models.IntegerField() rather than primary key models.AutoField()
-------------------------------+-----------------------
Reporter: paulcdejean | Owner: nobody
Type: Uncategorized | Status: new
Component: Utilities | Version: 1.7
Severity: Normal | Keywords: inspectdb
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------
My models.py was full of stuff like:
day_id = models.IntegerField(primary_key=True)
Due to the naming convention that we prefer for our primary keys.

I actually wasn't aware of AutoField until just now, and thought that
primary keys not being made auto increment was by design for some reason.

--
Ticket URL: <https://code.djangoproject.com/ticket/23748>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 2, 2014, 3:10:14 PM11/2/14
to django-...@googlegroups.com
#23748: inspectdb converts auto increment primary keys into primary key
models.IntegerField() rather than primary key models.AutoField()
-------------------------------+--------------------------------------
Reporter: paulcdejean | Owner: nobody
Type: Uncategorized | Status: closed
Component: Utilities | Version: 1.7
Severity: Normal | Resolution: invalid

Keywords: inspectdb | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* status: new => closed
* needs_better_patch: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_docs: => 0


Comment:

None of the Django core backends support `AutoField` introspection yet.
However, `inspectdb` should add a hint as a comment (`# AutoField?`) in
the resulting output. Apparently, it's the best we can do currently.

If you think you can add `AutoField` introspection in some Django core
backend or if the comment mentioned above is not present, feel free to
reopen the ticket with some more information.

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

Django

unread,
Nov 11, 2014, 4:04:40 AM11/11/14
to django-...@googlegroups.com
#23748: inspectdb converts auto increment primary keys into primary key
models.IntegerField() rather than primary key models.AutoField()
-------------------------------+--------------------------------------
Reporter: paulcdejean | Owner: nobody
Type: Uncategorized | Status: closed
Component: Utilities | Version: 1.7
Severity: Normal | Resolution: invalid

Keywords: inspectdb | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by paulcdejean):

Replying to [comment:1 claudep]:


> None of the Django core backends support `AutoField` introspection yet.
However, `inspectdb` should add a hint as a comment (`# AutoField?`) in
the resulting output. Apparently, it's the best we can do currently.
>
> If you think you can add `AutoField` introspection in some Django core
backend or if the comment mentioned above is not present, feel free to
reopen the ticket with some more information.

Which core backends don't support autofield introspection?

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

Django

unread,
Nov 11, 2014, 5:37:41 AM11/11/14
to django-...@googlegroups.com
#23748: inspectdb converts auto increment primary keys into primary key
models.IntegerField() rather than primary key models.AutoField()
-------------------------------+--------------------------------------
Reporter: paulcdejean | Owner: nobody
Type: Uncategorized | Status: closed
Component: Utilities | Version: 1.7
Severity: Normal | Resolution: invalid

Keywords: inspectdb | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by claudep):

There is a `can_introspect_autofield` flag set to `False` in
`django/db/backends/__init__.py`, and this flag is not set anywhere else
(see c89d80e2cc9bf1f401aa3af4047bdc6f3dc5bfa4). I'm not saying that core
backends *cannot* introspect `AutoField`, but currently none of them
implement the needed discovery code.

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

Django

unread,
Nov 13, 2014, 12:57:42 PM11/13/14
to django-...@googlegroups.com
#23748: inspectdb should introspect autofield
-------------------------------------+-------------------------------------
Reporter: paulcdejean | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 1.7
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: inspectdb | Someday/Maybe

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* status: closed => new
* resolution: invalid =>
* component: Utilities => Database layer (models, ORM)
* type: Uncategorized => New feature
* stage: Unreviewed => Someday/Maybe


Comment:

Opening this for if someone wants to implement the introspection. (Though,
it's not a bug.)

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

Django

unread,
Nov 19, 2014, 4:36:05 PM11/19/14
to django-...@googlegroups.com
#23748: inspectdb should introspect autofield
-------------------------------------+-------------------------------------
Reporter: paulcdejean | Owner: nobody

Type: New feature | Status: new
Component: Database layer | Version: 1.7
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: inspectdb | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by claudep):

* has_patch: 0 => 1
* stage: Someday/Maybe => Accepted


Comment:

PR for MySQL and PostgreSQL: https://github.com/django/django/pull/3579

--
Ticket URL: <https://code.djangoproject.com/ticket/23748#comment:5>

Django

unread,
Nov 20, 2014, 10:14:31 AM11/20/14
to django-...@googlegroups.com
#23748: inspectdb should introspect autofield
-------------------------------------+-------------------------------------
Reporter: paulcdejean | Owner: nobody

Type: New feature | Status: new
Component: Database layer | Version: 1.7
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: inspectdb | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by Claude Paroz <claude@…>):

In [changeset:"11662022be261d9a926b763e9fecc9f55ac3514a"]:
{{{
#!CommitTicketReference repository=""
revision="11662022be261d9a926b763e9fecc9f55ac3514a"
Added AutoField introspection for MySQL

Refs #23748.
}}}

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

Django

unread,
Nov 20, 2014, 10:14:31 AM11/20/14
to django-...@googlegroups.com
#23748: inspectdb should introspect autofield
-------------------------------------+-------------------------------------
Reporter: paulcdejean | Owner: nobody

Type: New feature | Status: new
Component: Database layer | Version: 1.7
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: inspectdb | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by Claude Paroz <claude@…>):

In [changeset:"1a63093e22a18688914a82651c461e2ea89cd564"]:
{{{
#!CommitTicketReference repository=""
revision="1a63093e22a18688914a82651c461e2ea89cd564"
Added AutoField introspection for PostgreSQL

Refs #23748.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23748#comment:7>

Django

unread,
Nov 24, 2014, 6:23:51 AM11/24/14
to django-...@googlegroups.com
#23748: inspectdb should introspect autofield
-------------------------------------+-------------------------------------
Reporter: paulcdejean | Owner: nobody

Type: New feature | Status: new
Component: Database layer | Version: 1.7
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: inspectdb | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0

Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by timgraham):

* has_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/23748#comment:8>

Django

unread,
Jul 14, 2017, 7:05:42 AM7/14/17
to django-...@googlegroups.com
#23748: inspectdb should introspect autofield
-------------------------------------+-------------------------------------
Reporter: Paul Dejean | Owner: nobody

Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: inspectdb | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* version: 1.7 => master


Comment:

In
[https://github.com/django/django/commit/9af6c97504ef2b06dd5292d03ea94d3eb5d8c4d6
9af6c97504ef2b06dd5292d03ea94d3eb5d8c4d6]

Added AutoField introspection on Oracle.

--
Ticket URL: <https://code.djangoproject.com/ticket/23748#comment:9>

Django

unread,
Jul 14, 2017, 9:32:13 AM7/14/17
to django-...@googlegroups.com
#23748: inspectdb should introspect autofield
-------------------------------------+-------------------------------------
Reporter: Paul Dejean | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: inspectdb | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham):

It looks like the remaining task for this ticket is to add introspection
on SQLite (if the column definition includes AUTOINCREMENT).

--
Ticket URL: <https://code.djangoproject.com/ticket/23748#comment:10>

Django

unread,
Jan 7, 2019, 6:22:43 PM1/7/19
to django-...@googlegroups.com
#23748: inspectdb should introspect autofield
-------------------------------------+-------------------------------------
Reporter: Paul Dejean | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: inspectdb | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Nick Pope):

Replying to [comment:10 Tim Graham]:


> It looks like the remaining task for this ticket is to add introspection
on SQLite (if the column definition includes AUTOINCREMENT).

So I've looked into rounding out the introspection support for AutoField -
here is a [https://github.com/django/django/pull/10825 PR] for SQLite.

It really turns out to be quite simple. According to the documentation
`AUTOINCREMENT` can only be used with `INTEGER PRIMARY KEY` which is
automatically an alias of `ROWID` that actually provides unique
incremented values. `AUTOINCREMENT` merely changes the algorithm used, but
is usually not necessary and ought to be avoided. See
[https://www.sqlite.org/autoinc.html here] and
[https://www.sqlite.org/lang_createtable.html#rowid here] for details.

There are two things to be aware of:

1. It is not possible to introspect `BigAutoField` as `BIGINT PRIMARY KEY`
cannot have `AUTOINCREMENT` and does not alias `ROWID`. I disabled the
test for SQLite.
2. No effort is made to check for composite primary keys, but I don't
think we do for other backends. We just assume the first `INTEGER PRIMARY
KEY` column is the `AutoField`.

--
Ticket URL: <https://code.djangoproject.com/ticket/23748#comment:11>

Django

unread,
Jan 8, 2019, 2:21:58 PM1/8/19
to django-...@googlegroups.com
#23748: inspectdb should introspect autofield
-------------------------------------+-------------------------------------
Reporter: Paul Dejean | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: inspectdb | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/23748#comment:12>

Django

unread,
Jan 8, 2019, 5:23:19 PM1/8/19
to django-...@googlegroups.com
#23748: inspectdb should introspect autofield
-------------------------------------+-------------------------------------
Reporter: Paul Dejean | Owner: Nick Pope
Type: New feature | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: inspectdb | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Nick Pope):

* owner: nobody => Nick Pope
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/23748#comment:13>

Django

unread,
Jan 9, 2019, 10:32:29 AM1/9/19
to django-...@googlegroups.com
#23748: inspectdb should introspect autofield
-------------------------------------+-------------------------------------
Reporter: Paul Dejean | Owner: Nick Pope
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: inspectdb | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"a35d2a4510d5beec398b1007aaa26492d6aedf97" a35d2a45]:
{{{
#!CommitTicketReference repository=""
revision="a35d2a4510d5beec398b1007aaa26492d6aedf97"
Refs #23748 -- Added AutoField introspection for SQLite.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23748#comment:14>

Django

unread,
Jan 9, 2019, 10:33:18 AM1/9/19
to django-...@googlegroups.com
#23748: inspectdb should introspect autofield
-------------------------------------+-------------------------------------
Reporter: Paul Dejean | Owner: Nick Pope
Type: New feature | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed

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

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

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


--
Ticket URL: <https://code.djangoproject.com/ticket/23748#comment:15>

Reply all
Reply to author
Forward
0 new messages