I have a table called Message. Each message may be associated with an Invoice table or an Rfp table, but not both. I'm struggling with the best way to implement this:
One approach is for the
Messagetable to have foreign keys to bothInvoiceandRfptables. One FK would be valid while the other must be NULL. (Single-Table Inheritance.) But this seems rather awkward. In addition to the unused column, I need to find ways to prevent cases where the FKs are either both used or both NULL. And this would need to be repeated for every single message.Another approach is to create a joining table. In this case, my
Messagetable would have an FK to the joining table, and theInvoiceandRfptables would also have an FK to the joining table. However, the problem here is that, given a reference to the joining table, it is awkward to find the relatedInvoiceorRfptable because I wouldn't know which contains the FK. So, here, I need to resort to other steps to know how to find the related table, such as adding a column to signify which table is related, for which it is hard to create a constraint.Finally, I could create two types of
Messagetables. This solves the problems described above, but this causes problems for our app because we have logic in places that needs to add messages without knowing what type they are. (We can only have an FK to the related table.)
Can anyone offer some tips here. None of these ways are ideal, but perhaps there are aspects I've failed to consider when choosing between the two. Or perhaps there is a better approach altogether.