Opened 10 years ago

Closed 6 years ago

#23748 closed New feature (fixed)

inspectdb should introspect autofield

Reported by: Paul Dejean Owned by: Nick Pope
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: inspectdb
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (15)

comment:1 by Claude Paroz, 10 years ago

Resolution: invalid
Status: newclosed

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.

in reply to:  1 comment:2 by Paul Dejean, 10 years ago

Replying to 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? It must be sqlite3 or oracle, since MySQL supports everything iirc, and Postgre supports everything and then some.

Last edited 10 years ago by Paul Dejean (previous) (diff)

comment:3 by Claude Paroz, 10 years ago

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.

comment:4 by Collin Anderson, 10 years ago

Component: UtilitiesDatabase layer (models, ORM)
Resolution: invalid
Status: closednew
Summary: inspectdb converts auto increment primary keys into primary key models.IntegerField() rather than primary key models.AutoField()inspectdb should introspect autofield
Triage Stage: UnreviewedSomeday/Maybe
Type: UncategorizedNew feature

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

comment:5 by Claude Paroz, 10 years ago

Has patch: set
Triage Stage: Someday/MaybeAccepted

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

comment:6 by Claude Paroz <claude@…>, 10 years ago

In 11662022be261d9a926b763e9fecc9f55ac3514a:

Added AutoField introspection for MySQL

Refs #23748.

comment:7 by Claude Paroz <claude@…>, 10 years ago

In 1a63093e22a18688914a82651c461e2ea89cd564:

Added AutoField introspection for PostgreSQL

Refs #23748.

comment:8 by Tim Graham, 10 years ago

Has patch: unset

comment:9 by Mariusz Felisiak, 7 years ago

Version: 1.7master

In 9af6c97504ef2b06dd5292d03ea94d3eb5d8c4d6

Added AutoField introspection on Oracle.

comment:10 by Tim Graham, 7 years ago

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

in reply to:  10 comment:11 by Nick Pope, 6 years ago

Replying to 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 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 here and 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.

comment:12 by Mariusz Felisiak, 6 years ago

Has patch: set

comment:13 by Nick Pope, 6 years ago

Owner: changed from nobody to Nick Pope
Status: newassigned

PR has been updated following reviews.
I have used Refs instead of Fixed, so this ticket will need closing once merged as SQLite is the final backend to be implemented.

Last edited 6 years ago by Nick Pope (previous) (diff)

comment:14 by Tim Graham <timograham@…>, 6 years ago

In a35d2a45:

Refs #23748 -- Added AutoField introspection for SQLite.

comment:15 by Tim Graham, 6 years ago

Resolution: fixed
Status: assignedclosed
Note: See TracTickets for help on using tickets.
Back to Top