erd.sketch

SQL에서 Prisma으로: DDL 스크립트로 모델 만들기

아래는 예시 그림이 아니라 왼쪽 스크립트에 대한 생성기의 실제 출력입니다. 편집기에서 「Codegen → Prisma」을 누르면 같은 코드가 나옵니다.

입력schema.sql
CREATE TABLE users (
    id BIGINT NOT NULL PRIMARY KEY,
    email       VARCHAR(255) NOT NULL UNIQUE,
    full_name   VARCHAR(120),
    is_active   BOOLEAN NOT NULL,
    created_at  TIMESTAMP NOT NULL
);

CREATE TABLE orders (
    id BIGINT NOT NULL PRIMARY KEY,
    user_id     BIGINT NOT NULL REFERENCES users(id),
    total       DECIMAL(12,2) NOT NULL,
    note        TEXT,
    placed_at   TIMESTAMP NOT NULL
);
결과schema.prisma
model Users {
  id         BigInt      @id
  email      String      @db.VarChar(255) @unique
  fullName   String?     @db.VarChar(120) @map("full_name")
  isActive   Boolean     @map("is_active")
  createdAt  DateTime    @map("created_at")

  orders     Orders[]

  @@map("users")
}

model Orders {
  id        BigInt      @id
  userId    BigInt      @map("user_id")
  total     Decimal     @db.Decimal(12, 2)
  note      String?     @db.Text
  placedAt  DateTime    @map("placed_at")

  users     Users       @relation(fields: [userId], references: [id])

  @@map("orders")
}
편집기 열기

스크립트는 브라우저에 남습니다. 해석도 생성도 사용자의 기기에서 이뤄지고 서버로는 아무것도 가지 않습니다.

타입이 어떻게 대응되나

SQL에서모델에서
BIGINTBigInt
VARCHAR(255)String @db.VarChar(255)
BOOLEANBoolean
TIMESTAMPDateTime
DECIMAL(12,2)Decimal @db.Decimal(12, 2)
TEXTString? @db.Text
널 허용 필드
String?
컬럼 이름
@map("full_name")
관계
@relation(fields: [userId], references: [id])