I recently had the very unpleasant experience of the postgresql sequence falling out of sync with the table id in a rails app. Until this happened, rails 'magic' meant that I had never known what a postgresql sequence was - it had always happened automagically. Even more alarmingly, I didn't even know when I had caused this 'out of sync' issue to occur (it was only when later records were created that the errors were thrown)
I managed to wade my way through the drama. But now I want to understand all the possible causes of such a severe problem as the postgresql sequence falling out of sync with the id in a rails table. I want to know this so I can avoid it in the future.
I caused it to happen by creating new records manually where I specified the id. E.g. User.create(id: 4566, name: "Jo", email: "jo@gmail.com")
(importantly, creating the record without specifying the id would have avoided the issue i.e. User.create(name: "Jo", email: "jo@gmail.com")
My question: in addition to specifying ids in newly created records, what other things does a rails dev need to know will cause this issue?