Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMaryAnn Eleanya
8,626 PointsMy post ID isn't updating
Every time I delete a post and add a new one the post id isn't updating. It just treats the post like a new instance. For example, I only have three posts in total but their post id says 11, 12, 13. How can I delete the post id so that it can start counting from ID:1.
2 Answers
Rogier Nitschelm
iOS Development Techdegree Student 5,461 PointsThe ID is not something you should care about in most cases. The assignment of id's is done by the database for you and this behaviour is expected. The id columns in your models (post
in this case) are automatically incremented every time a new record is made. Old id's are not being reused.
This may seem odd at first but to give you an idea of why this is a good thing. Imagine your database consists of two tables:
- users
- bank accounts
imagine if you are user with id 10, and you have a bank account that refers to your user (by id). Imagine if one day you delete your user account. Your id would be available again. Imagine if the next person to make an account gets assigned your old id of 10. Your bank account would then be associated with the new user. And he might get access to your information.
It still is possible to reuse id's by manually setting the id on creation of a record or updating a record, but its not recommended. I recommend letting your database handle the assignment of id's.
Jay McGavren
Treehouse TeacherJay McGavren
Treehouse TeacherGreat explanation, Rogier Nitschelm !