Skip to main content

Relationships

We support 2 ways of creating relationships:

  • Typing DBML code (docs)
  • Interacting directly on the diagrams itself, by dragging from field to field.

Relationship types:

  • < one-to-many. E.g: Ref: countries.code < users.country
  • > many-to-one. E.g: Ref: posts.user_id > users.id
  • - one-to-one. E.g: Ref: users.id - user_infos.user_id
  • <> many-to-many. E.g: Ref: posts.id <> tags.id

Optional relationship:

  • Add ? next to the side of the operator that is optional, meaning a row on the other side does not need a match. E.g: Ref: posts.editor_id >? users.id — a post may have no editor.
  • Works with every operator, so ?>, >?, ?>?, -?, <? and ?<>? are all valid.
  • To learn when to reach for each one, see the tutorial on optional relationships.

The example below uses all of them: countries to users is one-to-many, posts to users is many-to-one, users to user_infos is one-to-one, posts to tags is many-to-many, and a post's editor is optional.

Ref: countries.code < users.country       // one-to-many
Ref: posts.user_id > users.id // many-to-one
Ref: users.id - user_infos.user_id // one-to-one
Ref: posts.id <> tags.id // many-to-many
Ref: posts.editor_id >? users.id // optional

// or use inline relationships
Table posts {
id int [primary key]
user_id int [ref: > users.id]
editor_id int [ref: >? users.id]
...
}