This is my table, GameAdmin:
game_id company_id user_id
1 5 NULL
1 5 NULL
1 NULL 2
1 NULL 3
1 NULL 3
It links games to entities that can edit them (either a company or a user).
I have a UNIQUE index on all columns, but as you can see it’s not working as expected.
What is wrong? Is it because of the NULLs?
I know I could make it work by changing the structure to:
game_id admin_type admin_id
1 company 5
1 company 5
1 user 2
1 user 3
1 user 3
But that’s not compatible with my JPA/Hibernate setup, or at least very inconvenient, because it doesn’t allow me to set the relations like this:
@ManyToOne(optional=true)
private User user;
@ManyToOne(optional=true)
private Company company;
Oh, the solution is so simple. I split the constraint up, so there’s one for
game_idandcompany_id, and forgame_idanduser_id.