Many-to-many relationships in Mermaid ER diagrams

When to draw a many-to-many line and when to show the junction table, how to key and label it, and how to model attributes, roles, and self-references on the relationship.

Mermaid diagrams · Updated November 24, 2026 · 4 min read

Students take many courses, and each course has many students. Tags label many posts, and each post has many tags. Many-to-many relationships are common in real systems, and they are where ER diagrams most often hide the important detail. A relational database can't store one directly: it needs a third table, the junction table, and that table usually ends up holding facts of its own.

This guide shows both ways to draw the relationship and how to choose between them.

Two ways to draw it

The conceptual form is one line with a crow's foot at each end:

erDiagram
  STUDENT }o--o{ COURSE : "enrolls in"

It reads cleanly: a student enrolls in zero or more courses, and a course has zero or more students. It's the right picture for a conversation about the domain, before anyone has decided on tables.

The physical form shows the junction table that implements it:

erDiagram
  STUDENT ||--o{ ENROLLMENT : has
  COURSE ||--o{ ENROLLMENT : has
  STUDENT {
    int id PK
    string name
  }
  ENROLLMENT {
    int student_id PK, FK
    int course_id PK, FK
    date enrolled_on
    string grade
  }
  COURSE {
    int id PK
    string code UK
  }

The many-to-many line has become two one-to-many lines. Each enrollment belongs to exactly one student and exactly one course; each student and each course has zero or more enrollments.

Choose the form for the reader

Use the conceptual line when:

  • the audience is talking about the business, not the schema;
  • the relationship carries no data of its own; or
  • the diagram is an overview where a junction table would only add a box.

Show the junction table when:

  • the relationship has attributes, like enrolled_on and grade, which belong to neither the student nor the course;
  • someone will write queries, migrations, or API code from the diagram; or
  • the junction table has its own name in the product, such as membership, subscription or enrollment. That's a sign it's a real entity, not plumbing.

A useful test: if you'd ever need to answer "when did this happen?" or "who did this?" about the relationship, it's an entity. Draw the table.

Key and line style

There are two common ways to key a junction table, and the diagram should say which one you chose.

A composite primary key of the two foreign keys, as in ENROLLMENT above, guarantees one row per pair. Mark both columns PK, FK. Because the junction's identity depends on both parents, the relationships are identifying, drawn as solid lines (--).

A surrogate key gives the junction its own id, often with a unique constraint across the pair. The relationships are then non-identifying, drawn dashed (..), and the pair should be marked UK if it must be unique:

erDiagram
  USER_ACCOUNT ||..o{ MEMBERSHIP : holds
  TEAM ||..o{ MEMBERSHIP : grants
  MEMBERSHIP {
    int id PK
    int user_id FK, UK
    int team_id FK, UK
    string role
    date joined_on
  }

Mermaid can't group two columns into a single unique key, so UK on both reads as "unique together" only if your team agrees it does. Say so in a comment or a note next to the diagram if it matters.

Label from each side

Junction-table diagrams often end up with every line labelled has, which tells the reader nothing. Use the verbs people actually say:

  • STUDENT ||--o{ ENROLLMENT : "registers"
  • COURSE ||--o{ ENROLLMENT : "accepts"
  • USER_ACCOUNT ||..o{ MEMBERSHIP : "holds"
  • TEAM ||..o{ MEMBERSHIP : "grants"

If a junction carries a role, such as role in MEMBERSHIP, say so on the entity, not on the lines. A diagram can't show that only one owner per team is allowed; that rule belongs in a note or in the constraint itself.

Self-referencing many-to-many

People follow people; parts contain parts. The junction table has two foreign keys to the same entity, and each needs a name that says which role it plays:

erDiagram
  PERSON ||--o{ FOLLOW : "follows (follower_id)"
  PERSON ||--o{ FOLLOW : "is followed (followed_id)"
  FOLLOW {
    int follower_id PK, FK
    int followed_id PK, FK
    datetime since
  }

Two relationships connect the same pair of entities, and the labels are what tell them apart. Putting the column name in the label saves the reader from guessing which line is which.

What importers draw

Generated diagrams follow the source, so check which form you got:

  • A SQL schema has no many-to-many syntax, so SQL import always draws the junction table, with each relationship labelled by its foreign-key columns. Solid lines mean the foreign key is part of the junction's primary key.
  • A Prisma schema can declare an implicit many-to-many relation: a list field on both models and no @relation fields. Import from GitHub draws it as a conceptual }o..o{ line, because Prisma manages the hidden junction table itself.

You can switch between the forms afterwards. In Manual mode, add the junction entity with the toolbar, drag a + from it to each parent, and delete the conceptual line. Or go the other way for an overview: delete the junction entity, which removes its two relationships, and drag a + between the parents, then set both ends to Zero or more.

For the meaning of each marker, see the ER relationship cheat sheet.

More guides