-1

When I delete an existing record from database and recreated a exactly same record through Django admin interface, the id value shows in Django admin interface is not continuous. For instance, the record with id value is 6 and the previous one is 5, then I delete the one with id 6. When I recreate it, the id value becomes 7 instead of 6. I think it supposes to be 6. It this a error and how can I fix this issue?

pipi
  • 705
  • 1
  • 8
  • 16

1 Answers1

2

That is the correct behaviour. Primary keys should not get re-used, especially to avoid conflicts when it has been referenced in other tables.

See this SO question for more info about it: How to restore a continuous sequence of IDs as primary keys in a SQL database?

If you really want to reset the Auto Increment of PK, you can do ALTER TABLE tablename AUTO_INCREMENT = 1 (for MySQL). Other DBs may have different limitations like not being able to reset to any value lower than the highest used, even if there are gaps (MySQL:InnoDB).

Community
  • 1
  • 1
aneroid
  • 12,983
  • 3
  • 36
  • 66