I'm currently designing a database model and I've come across an issue, where I'd like some more input on what's considered the proper way of doing things.
In the example below I have two tables, Persons and Roles. Now 1 person may be assigned 0 or more roles. This is pretty straight forward with a many-to-many relationship, but the tricky part comes when you want to allow the user to select which role he is currently "operating" as.
Say the user got a selection of 5 roles to choose from, and depending on which role he currently has chosen, the application will present him with different views / menus / buttons / etc.
I see two solutions here:
1: (See top part of image linked below)
In the persons table, you include a nullable FK to the many-to-many relationship (the association entity).
From Wikipedia on Associative entity
It seems ok for "other entities" to reference this relationship. I'm just not entirely sure if this includes the Roles and Persons entities.
What I like about this design is that it's pretty easy to get a list of all allowed roles for a person, and then you simply point to one of those allowed roles, saying "you're the current/active one". This also feels like a good way to make sure that the user can only have a role as current, if he has access to the role... Only, I also see potential disaster here: What if, the FK in the persons table references that relationship of another user. The DB design would allow this. It would only be code that prevents it.
2: (See bottom part of image linked below)
I keep the associative entity simple, and instead I have FK in persons table that points to a specific role in the Roles table.
The problem here is that I get even less help from the DB design with ensuring that the "current role" of a person, is a role that is actually "allowed".
Any thoughts on this would be greatly appreciated =)
UPDATE: I'll include another variation of the same issue.
Two entities: WorkOrders and Sites
- 1 Site may have access to many Work orders
- 1 WorkOrder may be accessed by many Sites
So we have a many-to-many relationship
The issue: How do we best store in database which Site is the Owner of a work order, when there can only be 1 Site that Owns a work order.
Do we have "OwnerSiteId" as a column in the WorkOrder table, or would it be ok to reference the many-to-many relationship instead, saying "that one" is the owner.