ForgelivoDocs
Browse documentation

Model relationships

Understand one-to-one, one-to-many, and many-to-many connections.

A relationship describes how records are connected. It lets an application associate information without repeating the full contents of one record inside another.

One-to-many

One project can have many tasks, while each task belongs to one project. The Task record stores the identifier of its project:

Project: { "id": "project-42", "name": "Website launch" }

Task:    { "id": "task-101", "projectId": "project-42" }

Task:    { "id": "task-102", "projectId": "project-42" }

Both tasks point to the same project. The project-to-task relationship is one-to-many; the task-to-project relationship is many-to-one. These describe the same connection viewed from opposite sides.

One-to-one

A one-to-one relationship associates a record with at most one record of another type. For example, a user can have one profile, and each profile belongs to one user. A uniqueness rule on the user reference can prevent multiple profiles from referring to the same user.

Decide whether the relationship is required. A required profile reference and an optional profile reference describe different rules about which records can exist.

Many-to-many

A person can belong to many projects, and a project can have many people. A separate membership model can represent each association:

Membership: { "personId": "person-7", "projectId": "project-42" }

Membership: { "personId": "person-7", "projectId": "project-99" }

Membership: { "personId": "person-8", "projectId": "project-42" }

These records show that person-7 belongs to two projects, while project-42 has two people. The membership can also hold information about the relationship itself, such as the person's role in that project.

Keep references consistent

Choose the relationship

Ask how many records can be associated on each side. If both sides allow many, use an association model. If only one side allows many, store the reference on that side. If both sides allow at most one, use a one-to-one relationship with an appropriate uniqueness rule.