DBML → SQL:由结构描述生成 DDL
结构不必以 SQL 形式送来。下面是真实的转换:左边是 DBML 描述,右边是编辑器由它生成的结果。
输入schema.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']
}
}输出schema.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);打开编辑器
脚本留在浏览器里:解析和生成都在您的机器上进行,不会上传到服务器。
什么变成什么
| 源格式中 | 在 SQL 里 |
|---|---|
[pk, increment] | SERIAL PRIMARY KEY |
[not null, unique] | NOT NULL UNIQUE |
default: `now()` | DEFAULT now() |
ref: > users.id | REFERENCES users (id) |
indexes { } | CREATE INDEX idx_orders_user |