Reputation: 357
I deleted some objects belonging to one of my models in Django. Now when I create a new object, a wrong id is assigned to this object. This id is not successive. How can I address this problem? The example:
>>> Programmer.objects.all().values()
<QuerySet [{'id': 1, 'name': 'Linus Torvalds','occupation': 'Software enginner'},
{'id': 2, 'name': 'Tim Cook', 'occupation': 'CEO'},
{'id': 3, 'name': 'Elon Musk', 'occupation': 'Entrepreneur, engineer'}]>
>>> p4=Programmer(name='Mark Zuckerberg')
>>> p4.save()
>>> Programmer.objects.all().values()
<QuerySet [{'id': 1, 'name': 'Linus Torvalds', 'occupation': 'Software enginner'},
{'id': 2, 'name': 'Tim Cook', 'occupation': 'CEO'},
{'id': 3, 'name': 'Elon Musk', 'occupation': 'Entrepreneur, engineer'},
{'id': 15, 'name': 'Mark Zuckerberg', 'occupation': None}]>
Upvotes: 0
Views: 270
Reputation: 5228
When you are saving a new object I believe the Django just pulls the next unused id off of the database. Thus, if ids 4 through 14 were used, they don't get reused - even if you have since deleted those records.
Upvotes: 2