liveBook · Manning

One-to-many relationship

As explained in API Design Patterns

A one-to-many relationship is an association in which one entity may be related to multiple entities while each related entity has at most one corresponding entity on the other side.

A one-to-many relationship associates one resource with multiple resources while each related resource points to at most one resource on the other side. In a chat API, one User can author many Message resources, but each message has exactly one author. section 4.1.1

Chat messages and authors

Represent the relationship with an author field on each Message resource that references one specific User. Read in the opposite direction, the same structure means that a User may have many associated messages. This differs from many-to-many membership, where both sides can reference multiple resources. section 4.1.1

When modeling a relationship, clarify these points:

  • Identify which resource contains the reference and which resource may be associated with many instances. section 4.1.1
  • Distinguish one-to-many structure from one-to-one, many-to-many, and optional one-to-many relationships when drawing an entity relationship diagram. section 4.4
Figure 4.3 A message resource contains a reference to a specific user who authored the message.

Use the message-and-user diagram to visualize how a single Message references one User resource. section 4.1.1

How API Design Patterns covers it

How deep it goes
Defines reference direction with a chat example and contrasts many-to-many relationships, while deferring relationship mechanics.
Examples
Uses User and Message resources, a message author field, and a bookmark-and-folder entity relationship exercise.
What you can do after
Evaluate resource models and draw entity relationship diagrams using one-to-many connectors.
How it connects
Connects foreign keys, references, many-to-many relationships, one-to-one relationships, and optional one-to-many relationships.

Also explained in

Compare how 12 books explain one-to-many relationship →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of API Design Patterns. The section links point to the parts of the book it draws on.