Many to many

Many to many is the last type of relation that can exist, where one model object can relate to many other objects of another model type and vice versa. An example of this would be an employee working on many projects and a project having many employees. This kind of relation is known as the Sibling relation in Vapor.

A pivot table is needed; this is a table that stores the mapping of one model to many models of other types, and vice versa. The database tables for this relation would look like this:

employees:

Column Name Column Type
id Identifier
name String

projects:

Column Name Column Type
id Identifier
name String

employee_project:

Column Name Column Type
employee_id Identifier (Foreign Key)
project_id ...

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.