Move the id generation to lua.

It's no longer possible to generate custom ids.
All ids are autoincremented in save.lua. With this
change, there's one round trip less and if the
saving fails, no id is incremented.