erd.sketch

Prisma → SQL: DDL from a schema description

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

The inputschema.prisma
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id        Int      @id @default(autoincrement())
  email     String   @unique @db.VarChar(255)
  fullName  String?  @map("full_name")
  createdAt DateTime @default(now()) @map("created_at")
  orders    Order[]

  @@map("users")
}

model Order {
  id        Int      @id @default(autoincrement())
  userId    Int      @map("user_id")
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  total     Decimal  @db.Decimal(12, 2)
  placedAt  DateTime @map("placed_at")

  @@index([userId])
  @@map("orders")
}
What comes outschema.sql
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    email varchar(255) NOT NULL UNIQUE,
    full_name text,
    created_at timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP
);

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

ALTER TABLE orders ADD CONSTRAINT fk_orders_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE;

CREATE INDEX idx_orders_user_id ON orders (user_id);
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
@id @default(autoincrement())SERIAL PRIMARY KEY
String @db.VarChar(255)varchar(255)
@uniqueUNIQUE
@map("full_name")full_name
@default(now())DEFAULT CURRENT_TIMESTAMP
onDelete: CascadeREFERENCES users (id) ON DELETE CASCADE
@@map("users")CREATE TABLE users