# Text format reference

> The text view's format, DBML plus information types, column definitions and coloured areas, and how an edit in text is applied.

Source: https://docs.architure.dev/docs/text-format

The text format describes a whole [database](/docs/databases) as text: its schemas, tables, columns, relationships, enums and areas. You edit it in the canvas's text view, and agents receive it from the MCP tool `get_database`. It is [DBML](https://dbml.dbdiagram.io/docs/) (tables, columns, references, enums and table groups) with three additions:

- the column setting `type: <information type>`;
- the column setting `use: <column definition>`;
- table groups, which are the canvas's areas, with a `color` setting.

The format round-trips exactly: writing a database as text and reading the text back gives the same database and no changes. Every example on this page is checked that way.

What the text leaves out: descriptions (edit them in the canvas's details panel, or with the MCP tool `set_description`), table positions, area positions and sizes, and the canvas's view settings.

## An example

The "Lending library" sample database, as the text view shows it:

```dbml title="Lending library"
Enum copy_condition {
  new
  good
  worn
  damaged
}

Table branches {
  id uuid [pk, not null, type: identification]
  name text [not null, type: descriptive]
  city text [not null, type: descriptive]
  opens_at time [type: settings]
  created_at "timestamp with time zone" [not null, default: `now()`, type: audit]
}

Table members {
  id uuid [pk, not null, type: identification]
  card_number text [not null, unique, type: identification]
  full_name text [not null, type: descriptive]
  email text [not null, unique, type: descriptive]
  home_branch_id uuid [type: relationships]
  max_loans integer [not null, default: 5, type: settings]
  created_at "timestamp with time zone" [not null, default: `now()`, type: audit]
}

Table authors {
  id uuid [pk, not null, type: identification]
  full_name text [not null, type: descriptive]
  born_year integer
}

Table books {
  id uuid [pk, not null, type: identification]
  isbn text [not null, unique, type: identification]
  title text [not null, type: descriptive]
  genre text [type: categorization]
  branch_id uuid [not null, type: relationships]
  is_archived boolean [not null, default: false, type: status]
  created_at "timestamp with time zone" [not null, default: `now()`, type: audit]

  indexes {
    (branch_id) [name: 'books_branch_id_idx']
  }
}

Table book_authors {
  book_id uuid [pk, not null, type: relationships]
  author_id uuid [pk, not null, type: relationships]
  position integer [not null, default: 0, type: settings]
}

Table loans {
  id bigint [pk, not null, increment, type: identification]
  book_id uuid [not null, type: relationships]
  member_id uuid [not null, type: relationships]
  borrowed_at "timestamp with time zone" [not null, default: `now()`, type: descriptive]
  due_on date [not null, type: descriptive]
  returned_at "timestamp with time zone" [type: status]
  condition_out copy_condition [not null, default: 'good', type: status, use: copy_condition]
  condition_in copy_condition [type: status, use: copy_condition]

  indexes {
    (member_id) [name: 'loans_member_id_idx']
  }
}

Ref members_home_branch_id_fkey: members.home_branch_id > branches.id [delete: set null]
Ref books_branch_id_fkey: books.branch_id > branches.id [delete: restrict]
Ref book_authors_book_id_fkey: book_authors.book_id > books.id [delete: cascade]
Ref book_authors_author_id_fkey: book_authors.author_id > authors.id [delete: cascade]
Ref loans_book_id_fkey: loans.book_id > books.id [delete: restrict]
Ref loans_member_id_fkey: loans.member_id > members.id [delete: restrict]
Ref: members <> books

TableGroup Catalogue [color: teal] {
  authors
  books
  book_authors
}
```

The text lists the enums the database uses first, then the tables (schema by schema, in the order they were created), then the references, then the areas.

## Tables

```dbml
Table books {
  id uuid [pk, not null, type: identification]
  title text [not null, type: descriptive]
}

Table billing."invoice lines" {
  id bigint [pk, not null, increment, type: identification]
  amount numeric(10,2) [not null, type: descriptive]
}
```

- A table of the default schema `public` is written without its schema: `Table books`. A table of another schema is qualified: `Table billing.invoices`. A new schema is created when a table needs it; the text never deletes a schema.
- A name is bare when it is made of letters, digits and `_`; quote any other name in double quotes: `"invoice lines"`. The words `Table`, `Ref`, `Enum`, `TableGroup`, `Project`, `Note`, `Indexes` and `as` are quoted when they are used as names.
- Columns come first, one per line, then the table's `indexes` block.

## Columns

```dbml
Enum copy_condition {
  new
  good
  worn
  damaged
}

Table loans {
  id bigint [pk, not null, increment, type: identification]
  member_id uuid [not null, type: relationships]
  borrowed_at "timestamp with time zone" [not null, default: `now()`, type: descriptive]
  fee numeric(6,2) [not null, default: 0, type: descriptive]
  is_renewed boolean [not null, default: false, type: status]
  notes "character varying(500)"
  condition_out copy_condition [not null, default: 'good', type: status, use: copy_condition]
}
```

A column is its name, its data type and its settings in brackets, on one line. Columns are written in their order. The data type is written as in SQL; it is quoted when it contains spaces: `"timestamp with time zone"`, `"character varying(500)"`.

Settings, all optional, separated by commas, written in this order:

| Setting | Meaning |
| --- | --- |
| `pk` | Part of the primary key. Several `pk` columns make a composite key, in column order. `primary key` is read too |
| `not null`, `null` | Nullability: a column is nullable unless it says `not null` |
| `unique` | Unique |
| `increment` | Auto-increment (an identity column in PostgreSQL, `AUTO_INCREMENT` in MySQL) |
| `default: <value>` | A number (`5`), `true` or `false`, a string in single quotes (`'good'`, the SQL string literal), or any other SQL expression in backticks (`` `now()` ``). `default: null` means no default |
| `type: <information type>` | One of `identification`, `status`, `categorization`, `descriptive`, `relationships`, `settings`, `audit`. See [information types](/docs/information-types) |
| `use: <column definition>` | Links the column to the organization's [column definition](/docs/column-definitions) of that name |
| `note: '<text>'` | Read and ignored: descriptions are not part of the text |

A column with `use:` takes its data type, values, information type and description from the definition, so the text must agree with the definition: write the definition's data type (for an enum, its name), and write `type:` with the definition's information type, or leave `type:` out when the definition has none. A name that is not one of your organization's definitions is an error: create the definition on the **Column definitions** page first. To unlink a column, remove `use:`.

Strings use single quotes, and three single quotes (`'''`) for text over several lines. Inside quotes, a backslash escapes the next character (`\'`, `\\`, `\n`). `//` starts a comment to the end of the line, and `/* ... */` a comment over several lines.

## Indexes

Inside a table, after its columns:

```dbml
Table loans {
  id bigint [pk, not null, increment, type: identification]
  member_id uuid [not null, type: relationships]
  borrowed_at "timestamp with time zone" [not null, type: descriptive]

  indexes {
    (member_id) [name: 'loans_member_id_idx']
    (member_id, borrowed_at) [unique, name: 'loans_member_borrowed_key']
  }
}
```

Each line lists the index's columns in parentheses, in order, with the settings `unique` and `name`. An index you add without a name is named `<table>_<columns>_idx` (`_key` when it is unique). A bare column name without parentheses is read too. DBML's `(a, b) [pk]` marks those columns as the primary key. Indexes on expressions are not part of the model.

## References

A reference between columns is a foreign key, written from the referencing column to the referenced one:

```dbml
Table customers {
  id uuid [pk, not null, type: identification]
}

Table orders {
  id uuid [pk, not null, type: identification]
  customer_id uuid [not null, type: relationships]
}

Table invoices {
  id uuid [pk, not null, type: identification]
  order_id uuid [not null, unique, type: relationships]
}

Table order_lines {
  order_id uuid [pk, not null, type: relationships]
  line_number integer [pk, not null, type: identification]
}

Table shipments {
  id uuid [pk, not null, type: identification]
  order_id uuid [not null, type: relationships]
  line_number integer [not null, type: relationships]
}

Ref orders_customer_id_fkey: orders.customer_id > customers.id [delete: restrict, update: cascade]
Ref invoices_order_id_fkey: invoices.order_id - orders.id
Ref order_lines_order_id_fkey: order_lines.order_id > orders.id [delete: cascade]
Ref shipments_order_line_fkey: shipments.(order_id, line_number) > order_lines.(order_id, line_number)
```

`Ref`, then the constraint name (optional), a colon, the two ends and the operator, then the actions:

| Operator | Cardinality |
| --- | --- |
| `>` | Many-to-one: many rows of the left table point at one row of the right table |
| `<` | One-to-many: the same, written from the referenced side (`Ref: authors.id < books.author_id`) |
| `-` | One-to-one |
| `<>` | Many-to-many, only between tables |

Actions: `delete:` and `update:`, each `no action`, `restrict`, `cascade`, `set null` or `set default`. A composite foreign key lists its columns in parentheses on both sides, in the same order. A reference without a constraint name (`Ref: orders.customer_id > customers.id`) gets the database's usual name when you export SQL.

A reference between two tables, drawn before you know which columns join them, names the tables only:

```dbml
Table members {
  id uuid [pk, not null, type: identification]
}

Table books {
  id uuid [pk, not null, type: identification]
}

Ref: members <> books
```

The issues panel lists such a reference until you detail it into columns (rule `table_relationship_not_detailed`).

## Enums

```dbml
Enum copy_condition {
  new
  good
  worn
  damaged
}

Table loans {
  id bigint [pk, not null, increment, type: identification]
  condition_out copy_condition [not null, default: 'good', type: status, use: copy_condition]
}
```

An `Enum` block shows the values of a column definition the database uses, so that the text reads like DBML in other tools. Definitions belong to your organization: the text cannot create or change them. An `Enum` that is not one of your definitions, or that lists other values, is an error; change the values on the **Column definitions** page.

## Areas

An area of the canvas is a table group with a colour:

```dbml
Table authors {
  id uuid [pk, not null, type: identification]
}

Table books {
  id uuid [pk, not null, type: identification]
}

Table members {
  id uuid [pk, not null, type: identification]
}

TableGroup Catalogue [color: teal] {
  authors
  books
}

TableGroup "Members and loans" [color: amber] {
  members
}
```

Colours: `slate`, `red`, `orange`, `amber`, `yellow`, `lime`, `green`, `emerald`, `teal`, `cyan`, `sky`, `blue`, `indigo`, `violet`, `purple`, `fuchsia`, `pink`, `rose`. A new group without `color` is slate. A table belongs to at most one area, and a table listed in no group leaves its area. Removing a group deletes the area and keeps its tables.

## DBML from other tools

The text view also reads what other DBML tools write. What the model does not hold is read and ignored: `Project` blocks, `Note` blocks and `note:` settings, table aliases (`as B`) and table settings such as `headercolor`.

```dbml title="DBML from another tool"
Project library {
  database_type: 'PostgreSQL'
}

Table authors {
  id integer [primary key, increment]
  name varchar(100) [not null]
}

Table books as B [headercolor: #3498DB] {
  id integer [primary key, increment]
  title varchar(200) [not null, note: 'Shown in the catalogue']
  author_id integer

  Indexes {
    author_id
  }

  Note: 'One row per title the library owns.'
}

Ref: authors.id < books.author_id // one author, many books
```

Pasted this way, the tables arrive without information types: the issues panel lists the columns to type (rule `missing_information_type`).

## How an edit is applied

In the text view, edit the text, then choose **Apply** or press <kbd>Cmd</kbd> + <kbd>S</kbd> (<kbd>Ctrl</kbd> + <kbd>S</kbd> on Windows and Linux). **Discard** drops your edit. Until you edit, the text view follows the database as it changes, including your teammates' and agents' changes.

Architure compares the text with the database by name, using `schema.table.column` paths:

- an object with the same name keeps its identity and receives only what changed;
- a name that disappeared is deleted;
- a new name is created. A new table is placed under the existing ones, and a new area around its tables.

So **a rename in the text is a delete plus a create**: a renamed table gets a new identity, a new place on the canvas and no description, and a renamed column loses its description. Rename on the canvas, or with the MCP tool `update_column`, to keep them.

References are matched by their columns: changing a reference's name or actions updates it, and changing its columns replaces it. Indexes are matched by name.

Text that cannot be applied changes nothing. Its errors are marked at their line and column: a syntax error, an unknown setting, a reference or a group that names a missing table or column, an unknown column definition, or an `Enum` that differs from its definition.
