DBML ERD Generator — Visualize DBML Schemas
DBML (Database Markup Language) is the language the ERD Generator editor uses. Paste a .dbml file or a DBML snippet into the editor, and each Table, Enum, TableGroup and Ref line appears on the canvas as a table, an enum, a group or a relationship.
The editor reads DBML in your browser with the DBML grammar from @dbml/core, so the text stays on the page. Parse errors come with the line and column where the parser stopped.
How this tool handles it
The editor reads these elements:
Tablewith columns. Each column takes a type and settings in brackets:pk,primary key,not null,null,unique,increment,default:,note:andref:.Enumblocks, which the column type can refer to by name.TableGroupblocks, which group tables on the canvas.Projectblocks, which are accepted.Noteon a table or column. Acolor:token in a table note sets the table border color, and the rest of the note text stays as the table note.indexes { }blocks with names,uniqueand multi-column keys.Ref:lines with>,<,-and<>. A column can also declare its relationship inline with[ref: > table.column].
A Ref line accepts only delete: and update: inside its brackets. The editor reads label: and color: from an // @erd comment on the same line, so those settings go there.
Worked example
This schema has a project block, an enum, a table group, an inline reference, an index and a standalone reference with a delete rule:
Project shop {
database_type: 'PostgreSQL'
Note: 'demo'
}
Enum order_status {
pending [note: 'waiting']
paid
}
TableGroup billing {
orders
payments
}
Table orders {
id bigint [pk, increment]
status order_status [not null, default: 'pending']
customer_id bigint [ref: > customers.id]
Note: 'customer orders'
indexes {
(customer_id, status) [unique, name: 'uq_cust_status']
}
}
Table customers {
id bigint [pk]
email varchar(255) [unique]
}
Table payments {
id bigint [pk]
order_id bigint
}
Ref: payments.order_id > orders.id [delete: cascade, update: no action]The parser reads three tables (orders, customers, payments), one enum (order_status), one group (billing), the project shop, and two relationships: customers.id to orders.customer_id from the inline reference, and payments.order_id to orders.id from the Ref line.
Gotchas
1. A label inside the Ref brackets is a syntax error. Input:
Table a {
id int [pk]
}
Table b {
a_id int
}
Ref: b.a_id > a.id [label: 'owns']The parser reports: Expected "delete:", "update:", comment, or whitespace but "l" found. Move the label into the comment:
Ref: b.a_id > a.id [delete: cascade] // @erd color:#ff0000 label:"owns"2. An unknown column setting stops the parse. id int [pk, bogus] reports Expected "default:", "increment", "not null", "note:", "null", "pk", "primary key", "ref:", "unique", comment, or whitespace but "b" found. Remove the setting, or check the spelling.
3. A Ref must name a table that exists. Ref: a.id > missing.id fails with Can't find table. The same error appears when a Ref is left in the text after its table is renamed or removed.
4. Composite references use parentheses. Ref: a.(x, y) > b.(x, y) is read as one relationship over two columns, and the parser accepts it without errors.
Frequently asked questions
Which DBML elements does the diagram read?
Table with columns and settings, Enum, TableGroup, Project, Note on a table or column, indexes blocks with names and unique, and Ref lines in either the standalone form or the inline ref: column setting. Composite references such as a.(x, y) > b.(x, y) are read too.
How do I set a label or a color on a relationship?
Put the setting in a comment after the Ref line, using @erd, as in Ref: b.a_id > a.id [delete: cascade] // @erd color:#ff0000 label:"owns". The bracket part accepts only delete and update, so a label inside the brackets is a syntax error.
What does a DBML syntax error look like?
The parser stops at the first token it cannot read and names the tokens it expected. An unknown column setting, for example [pk, bogus], lists the settings that are allowed, such as pk, not null, unique, increment, default:, note: and ref:.
Can I convert DBML to SQL?
The DBML-to-SQL export lists MySQL, PostgreSQL, Microsoft SQL Server and JSON as targets, along with DBML itself. SQLite is not in the list.