Several bug fixes for unique constraint. Issue: #512
Fixed tests and added some more. Issue: #412