erd.sketch

DBML → SQL: DDL from a schema description

A schema does not have to arrive as SQL. Below is the real conversion: the DBML description on the left, what the editor makes of it on the right.

The inputschema.dbml
Project shop {
  database_type: 'PostgreSQL'
}

Table users {
  id          integer [pk, increment]
  email       varchar(255) [not null, unique]
  full_name   varchar(120)
  created_at  timestamp [not null, default: `now()`]
}

Table orders {
  id          integer [pk, increment]
  user_id     integer [not null, ref: > users.id]
  total       decimal(12,2) [not null]
  placed_at   timestamp [not null]

  indexes {
    (user_id, placed_at) [name: 'idx_orders_user']
  }
}
What comes outschema.sql
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    email varchar(255) NOT NULL UNIQUE,
    full_name varchar(120),
    created_at timestamp NOT NULL DEFAULT now()
);

CREATE TABLE orders (
    id SERIAL PRIMARY KEY,
    user_id integer NOT NULL REFERENCES users (id),
    total decimal(12,2) NOT NULL,
    placed_at timestamp NOT NULL
);

CREATE INDEX idx_orders_user ON orders (user_id, placed_at);
Open the editor

The script stays in the browser: both parsing and generation run on your machine, nothing goes to a server.

What turns into what

in the sourcein SQL
[pk, increment]SERIAL PRIMARY KEY
[not null, unique]NOT NULL UNIQUE
default: `now()`DEFAULT now()
ref: > users.idREFERENCES users (id)
indexes { }CREATE INDEX idx_orders_user