AVSoftware
AVSoftware

Reputation: 35

CoreData Model Schema and Relationships

I have a question with relationships in my CoreData model.

I have two Entities for now. 1. Flight, 2 Airport I have attached the screenshots of the model to this post.

The question I have is how do I related the flight to the airport. In my SQL Databased schema I have foreign keys for each but in CoreData how can I set up this relationship correctly.

Essentially the Flight has an Origin and a Destination.

Schema in Editor Style

enter image description here

enter image description here

Flight

airports relationship

enter image description here

Any help would be appreciated.

AV

Upvotes: 1

Views: 60

Answers (1)

pbasdf
pbasdf

Reputation: 21536

You can create two separate relationships between the two entities:

  1. An “origin” relationship from Flight to Airport (to-one), with a to-many inverse relationship from Airport to Flight, “flightsStartingHere”, and
  2. A “destination” relationship from Flight to Airport (also to-one), with a to-many inverse, “flightsTerminatingHere”.

Life gets more complicated if your flights have multiple stops. In that case I would model each Flight as having several Legs, and each Leg an origin Airport and a destination Airport. But the details will depend on the SQL database you are emulating.

Upvotes: 1

Related Questions