Fix assignment of IDs to multiple relations when one of them is empty an...

...d add test for it (and in general for ID assignment)
Exactly as much as it could have been empty before. This is bug-to-bug-compatible with the previous version, I just moved the code.