I handle members' roles in a table with this structure:
- id: id of the row
- id_member: integer, foreign key is 'id' column in 'members' table
- id_role: integer, foreign key is 'id' column in 'roles' table
- date_start: timestamp when this user gets the role
- date_end: timestamp when this user loses the role
When I add a role, the date_start
is set with current_timestamp
, and date_end
is null.
When I remove a role, the date_end
is set with current_timestamp
.
I don't want a user to have several roles at the same time, so initially I thought about setting a triple primary key: id_member
, id_role
and date_end
, but it appears I can't put a nullable column as primary key.
How could I change the structure of the table so that I can prevent a user having 2 active roles? I thought about adding a active
column but not only would it overcharge the structure, but also I won't be able to save 2 historical roles (if a user was ROLE3 during 4 different periods, for example).
Thanks in advance.