Fixed a bug where sometimes the wrong object-id was written to the
database, causing errors when reading records later on.