Database direct fixes

A couple of bugs discovered whilst writing integration tests.