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 |