Fix some integrity errors

The NDO SQL schema has some unique key constraints and this module violates some of them.

Here are some patches that help the module to produce unique keys.