How do I relate models? (Part One)

Once you’ve created multiple Core Data models, you can relate them to each other. For example, you are mostly working with organizations (Organization model) and would like to locate them on a map (Place model) and record people who are associated with them (Person model).

You might not have a location or people for every single organization, but you’d like to create fields where you can enter the data you’ve collected.

Under Project Settings, select the Configure tab and edit the model you’d like to have related to another model (in the example above, you’d choose the Organization model).

Click on the Relationships tab.

Click Add. You’ll see a list of models you’ve already created. Choose which model you’d like to relate to the one you’ve selected (for example you can relate Person to Organization).

Name this relationship so that when entering data, it’s clear what should be added. For example, “People who worked here” tells you that you’re going to list people who worked at this organization.

By default, you have a record in one model and are relating a single record from another model to it, so you can relate one person to this place. If you check the “Multiple” box, you can relate multiple people to this organization.

If you check the inverse box, you can relate these models in both directions. You will need to enter a name to describe this relationship on the other model. In this example, we’re relating places to a person, so the description could be, “Organization where this person worked.” Once again, by default, you are relating one record to another single record. Selecting both “multiple” and “inverse multiple,” one person can be related to multiple organizations and multiple people can be related to a single organization.

You will need to both Save this relationship and then, when this popup closes, Save the changes to this model.

To confirm that you’ve related two models, switch to Data Entry and go to your Organization model.

Add a new organization.

When you Save the record, the related Person model will appear.

You can add a related person to this record. Once related, you can use the arrow button to go to that person’s record.

If you created an inverse relationship, go to the related record to verify that this relationship is set up correctly. In this case, Org 1 appears on this person’s record.

To relate additional models, repeat this process.