#16992 closed Cleanup/optimization (fixed)
MySQL InnoDB AUTO_INCREMENT bug (next value to use forgotten at restart)
Reported by: | Owned by: | Keith Edmiston | |
---|---|---|---|
Component: | Documentation | Version: | 1.3 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
I recently ran into problems with Django's automatic id field.
When the latest added object in a table was deleted, a new object created
some time later reused the same id value as the deleted object.
It turned out that this was due to the fact that I had restarted the
MySQL server between the deletion and the creation, and the fact
that the InnoDB backend does not remember the AUTO_INCREMENT value
when restarted (instead recreating it as "max(id)+1").
See for example http://bugs.mysql.com/bug.php?id=727
I guess there is not much that Django can do to fix this, but I think
the MySQL/InnoDB documentation should mention this peculiarity.
Change History (11)
comment:1 by , 13 years ago
comment:2 by , 13 years ago
Component: | Database layer (models, ORM) → Documentation |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → Cleanup/optimization |
The MySQL devs say:
This behavior is a documented feature which will be fixed in the future at the same time a fast COUNT(*) is introduced to InnoDB.
That was in 2003; I'm not holding my breath. And I don't think we have to document every bug in MySQL.
That said, since it was decided for #10164 to enforce non-reusable primary keys, in this case, it's worth adding a paragraph to the MySQL notes.
comment:3 by , 13 years ago
See also #16961 for a related MySQL particularity regarding AUTO_INCREMENT
.
comment:5 by , 11 years ago
It looks like this is the MySQL ticket for this issue: http://bugs.mysql.com/bug.php?id=199
comment:6 by , 11 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:7 by , 11 years ago
Considering adding the following to the Databases.txt:
"InnoDB autoincrement counter is lost on a MySQL restart because it does not remember the AUTO_INCREMENT value, instead recreating it as "max(id)+1". This may result in an inadvertant reuse of a previous value."
comment:8 by , 11 years ago
Sounds good. I'd add it after "It's probably the best choice at this point."
However, note that the the InnoDB autoincrement counter is lost on a MySQL restart because it does not remember the ``AUTO_INCREMENT`` value, instead recreating it as "max(id)+1". This may result in an inadvertent reuse of :class:`~django.db.models.AutoField` values.
comment:9 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Fixed in c54fa1a7bc365fec79d4971bf22d5ad2799fde67.
#10164 reports similar behavior with sqlite3, but it is controllable with DDL by adding a
' AUTOINCREMENT'
suffix to the AutoField's DB field creation code.