问题
I am trying to design the backend and have the following use case.
I have flight information from point A to B and need to define a schema which supports different use cases.
I'm trying to find a good way to handle the case, when there are stopover via points.
For e.g. flight route for A -> B actually looks like this:
A -> C
C -> D
D -> B
so A -> B is one entity, but in turn, it is comprised of several legs.
My current design:
AirLeg table:
- id
- departure and arrival information
- viaPoints: BOOL
viaPoints table:
- id
- airLegId // FK into Airleg table
- similar departure and arrival information from airLeg table
// if viaPoints flag is True in AirLeg table, viaPoints table can be queried for, using airLegId table to retrieve intermediaries.
Is there a better way to deal with this ?
I thought I'll add the info I am storing about a one way trip or segment:
- AirLeg-id
- Departure Airport : FK into airports
- Arrival Airport : FK into airports
- Departure timestamp (in departure city's local time)
- Arrival timestamp (in arrival city's local time)
- flight duration of this airleg: static value
- flightId : FK into airlines yielding airline name and flight number
- Baggage Policy : text
- Misc (TEXT: Cancellation policy)
EDIT:
I added a related question and I think the answer to this problem will have to cater to both the requirements.
If there are multiple segments in a trip, price is defined for the complete trip and not individual segments
Similarly, the price for a round trip is specified as a unit and not individual components from A->B and back, B->A.
回答1:
I'd design it like this:
Journeys:
- ID
- Other info (billing, whatever)
Segments:
- ID
- JourneyID (FK)
- departure, arrival, etc
And an additional view
Journeys_View
- Journeys.*
- First departure
- Last arrival
回答2:
I'm trying to piece together the two questions, and it's not totally clear what you want to do - but I think it boils down to the following.
You have an itinerary, which is the parent item. An "itinerary" has multiple legs (question: do you want to deal with multi-part itinerarys, e.g. "London->Paris->New York->London"?). An itinerary has a price. The price is NOT the sum of the price of the legs, because return trips are cheaper than two one ways.
Itinerary
---------
ID
Price
Leg
----
Departure Airport : FK into airports
Arrival Airport : FK into airports
Departure timestamp (in departure city's local time)
Arrival timestamp (in arrival city's local time)
flight duration of this airleg: static value
flightId : FK into airlines yielding airline name and flight number
Baggage Policy : text
Misc (TEXT: Cancellation policy)
You could store price in a separate table - but you only need to do this if price changes independently of itinerary (e.g. if the price on Monday is $100, and on Tuesday it's $200).
I would encourage you not to use "magic numbers" in your database schema - instead of having the return leg be "-1", you should leave it NULL - there is no return leg. This makes your SQL a lot easier to read, and far less error prone - you don't depend on developers remembering that "-1" means there's no return leg, -2 meaning there's a provisionally booked leg etc.
来源:https://stackoverflow.com/questions/9017733/how-do-i-design-intermittent-via-points-for-travel-itinerary