Alternatives for the user relationship
What are the theoretical alternatives:
- you have a class
User with an association to Company and another one to Distributor. You also have a constraint between the two associations, that only one can be active at a given moment in time. Consequence: the association can change over time and a user once assigned to a company can later be assigned to a distributor.
- you have a general class
User with two specializations: CompanyUser inly associated with Company and DistributorUser associated with a distributor. Consequence: although a company user may change company over time, he/she might not change to a distributor (would require creation of a different user).
Choice of the alternative
What model seems the most accurate to you ? What would be the other differences between a company user and a distributor user that could justify the specialization ?
I do not have all the cards in hand, but intuitively I’d go for 1, which would be implemented exactly as you’ve described.
The approach 2 would be implemented either (also) as you’ve described (using the single inheritance table pattern) or with 2 tables (assuming User is abstract and using concrete table inheritance pattern, but the two tables might look very similar), or with 3 tables (using the class table inheritance) and a couple of additional relations requiring a lot of sql JOINs.
Now, looking at these other alternatives, unless company and distributor users would be very different, the additional tables seem to me as an overkill and I’d still stick to your initial approach, which seems to me a very good choice.
Other alternatives on the company/distributor side ?
One additional question worthwhile to investigate, however: are you sure that distributor and company are not two specialization of an abstract LegalEntity ?
In this case you could consider simplifying things on the user side by making only one relation between user and LegalEntity to which he/she belongs. You would then manage the legal entity the distributor and the company as tables of a class inheritance table scheme that implements the inheritance scheme between the classes.
May be this last alternative is an overkill, but depending on the similarities between distributor and company, you could the additional complexity could be compensate by more consistency and less redundancy in the code.