One to one (parent-child relation)

A one to one relation is when a model relates to one, and only one, other models. An example of such a relation would be that a Person model has one Government ID, and a Government ID can belong to one, and only one, Person. In this relation, both models have only one reference to each other, and no more. In Vapor, this type of relation is known as a Parent/Child relation. The database table for such a relation would look like this:

Persons:

Column Name Column Type
id Identifier
name String

government_ids:

Column Name Column Type
id Identifier
number Int
person_id Identifier (Foreign Key)

The following is how we would express this relation in our two model classes:

extension GovernmentId ...

Get Hands-On Full-Stack Development with Swift now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.