The problem arises from the following three tables -
Tour
- Id
TourPackage
- Id
- TourId
Traveller
- Id
- TourId
- PackageId
The foreign key constraints are in place. However, I also wish to enforce that Traveller's Package must also refer to the same Tour that the Traveller is referring.
Traveller.Tour = Traveller.Package.Tour
I might be able to easily enforce this on business layer, but could I do this in the database? Or should I rethink my design? Note that I don't plan to extract the foreign keys in a new, relationship table. I think that Tour and Package are part of a Traveller entity, and hence should reside in the entity itself.
UPDATE
My application runs within the context of a single Tour. Thus, all the data that I load will point to a single tour. Also, most of the data comes from Traveller table. So, it makes sense to have a direct TourId to the table. Anyways, I look at the relationships in this way.
- A tour has several packages.
- A traveller enlists himself for a tour.
- The traveller selects package for the subscribed tour.
So, I guess a traveller referring to tour as well as package is sensible. Am I wrong here?