Why does rails database id keep counting forward after destroying an intermediate item?
-
27-10-2019 - |
Question
The title might not be so clear, and anyway it's better to just look at this:
My sequence of creating/destroying items A, B and C is:
- Create A -->
id:1
- Create B -->
id:2
- Destroy B
- Create C -->
id:3
I've already destroyed B, C should be counted as id:2
right?
Solution
The underlying database column for id
is set to autoincrement. The id is generated at the database level, not by ActiveRecord
. If you want to know more, read up on SQL sequences or autoincrement .
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow