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)
follow-up: 2 comment:1 by , 10 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 by , 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.
comment:3 by , 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 , 10 years ago
Component: | Utilities → Database layer (models, ORM) |
---|---|
Resolution: | invalid |
Status: | closed → new |
Summary: | inspectdb converts auto increment primary keys into primary key models.IntegerField() rather than primary key models.AutoField() → inspectdb should introspect autofield |
Triage Stage: | Unreviewed → Someday/Maybe |
Type: | Uncategorized → New feature |
Opening this for if someone wants to implement the introspection. (Though, it's not a bug.)
comment:5 by , 10 years ago
Has patch: | set |
---|---|
Triage Stage: | Someday/Maybe → Accepted |
PR for MySQL and PostgreSQL: https://github.com/django/django/pull/3579
comment:8 by , 10 years ago
Has patch: | unset |
---|
comment:9 by , 7 years ago
Version: | 1.7 → master |
---|
In 9af6c97504ef2b06dd5292d03ea94d3eb5d8c4d6
Added AutoField introspection on Oracle.
follow-up: 11 comment:10 by , 7 years ago
It looks like the remaining task for this ticket is to add introspection on SQLite (if the column definition includes AUTOINCREMENT).
comment:11 by , 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:
- It is not possible to introspect
BigAutoField
asBIGINT PRIMARY KEY
cannot haveAUTOINCREMENT
and does not aliasROWID
. I disabled the test for SQLite. - 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 theAutoField
.
comment:12 by , 6 years ago
Has patch: | set |
---|
comment:13 by , 6 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:15 by , 6 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
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.