SQL to Prisma

SQL DDL to Prisma Schema Converter (Models, Relations & Enums)

Translate SQL CREATE TABLE DDL statements into clean Prisma ORM schema models, relations, field attributes, and enums.

Presets:
Database Provider:
SQL DDL Input(5 tables)
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

enum OrderStatus {
  PENDING
  PROCESSING
  PAID
  SHIPPED
  DELIVERED
  CANCELLED
}

enum UserRole {
  ADMIN
  CUSTOMER
  MERCHANT
}

model User {
  id           String        @id @default(uuid()) @db.Uuid
  email        String        @unique @db.VarChar(255)
  fullName     String?       @map("full_name") @db.VarChar(150)
  role         UserRole      @default("CUSTOMER")
  isVerified   Boolean       @default(false) @map("is_verified")
  metadata     Json?
  createdAt    DateTime      @default(now()) @map("created_at") @db.Timestamptz
  updatedAt    DateTime      @default(now()) @updatedAt @map("updated_at") @db.Timestamptz
  userProfile  UserProfile?
  orders       Order[]

  @@map("users")
}

model UserProfile {
  id               String   @id @default(uuid()) @db.Uuid
  userId           String   @unique @map("user_id") @db.Uuid
  avatarUrl        String?  @map("avatar_url") @db.VarChar(500)
  phoneNumber      String?  @map("phone_number") @db.VarChar(32)
  shippingAddress  String?  @map("shipping_address") @db.Text
  taxIdentifier    String?  @map("tax_identifier") @db.VarChar(64)
  user             User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@map("user_profiles")
}

model Product {
  id              Int          @id @default(autoincrement())
  sku             String       @unique @db.VarChar(64)
  title           String       @db.VarChar(200)
  description     String?      @db.Text
  unitPrice       Decimal      @default(0.00) @map("unit_price") @db.Decimal(12, 2)
  inventoryCount  Int          @default(0) @map("inventory_count")
  isActive        Boolean      @default(true) @map("is_active")
  createdAt       DateTime     @default(now()) @map("created_at") @db.Timestamptz
  orderItems      OrderItem[]

  @@index([sku])
  @@map("products")
}

model Order {
  id             String       @id @default(uuid()) @db.Uuid
  customerId     String       @map("customer_id") @db.Uuid
  orderNumber    String       @unique @map("order_number") @db.VarChar(64)
  status         OrderStatus  @default("PENDING")
  totalAmount    Decimal      @default(0.00) @map("total_amount") @db.Decimal(12, 2)
  taxAmount      Decimal      @default(0.00) @map("tax_amount") @db.Decimal(10, 2)
  shippingNotes  String?      @map("shipping_notes") @db.Text
  createdAt      DateTime     @default(now()) @map("created_at") @db.Timestamptz
  updatedAt      DateTime     @default(now()) @updatedAt @map("updated_at") @db.Timestamptz
  customer       User         @relation(fields: [customerId], references: [id], onDelete: Restrict)
  orderItems     OrderItem[]

  @@index([status])
  @@map("orders")
}

model OrderItem {
  id         Int      @id @default(autoincrement())
  orderId    String   @map("order_id") @db.Uuid
  productId  Int      @map("product_id")
  quantity   Int      @default(1)
  unitPrice  Decimal  @map("unit_price") @db.Decimal(12, 2)
  subtotal   Decimal  @db.Decimal(12, 2)
  order      Order    @relation(fields: [orderId], references: [id], onDelete: Cascade)
  product    Product  @relation(fields: [productId], references: [id], onDelete: Restrict)

  @@unique([orderId, productId])
  @@map("order_items")
}
Parsed Models5 models generated
Relations Inferred4 foreign keys resolved
Detected Enums2 typed enums

100% Client-Side Privacy

Zero server uploads or database connections. All SQL parsing, AST extraction, and Prisma schema generation occur strictly in your local browser sandbox.

Intelligent Relation & Key Resolution

Automatically resolves inline and composite foreign keys into typed Prisma @relation directives, bidirectional back-references, and compound @@id / @@unique constraints.

Multi-Dialect DDL Engine

Seamlessly handles PostgreSQL, MySQL, SQLite, and SQL Server syntax including SERIAL, UUID defaults, native types, composite indexes, and custom enums.

Technical Reference & Architecture Guide

SQL DDL to Prisma Schema Converter (Models, Relations & Enums) Developer Guide — Reference Guide

Translate SQL CREATE TABLE DDL statements into clean Prisma ORM schema models, relations, field attributes, and enums.

Key Parameters & Controls

SQL DialectEnum (PostgreSQL | MySQL | SQLite | SQL Server | CockroachDB)

Target SQL dialect used for parsing table definitions, specific data types (such as SERIAL, VARCHAR, DATETIME2, JSONB), and default constraint clauses.

Default: PostgreSQL
Naming & Case ConventionEnum (camelCase & PascalCase with @map | Preserve SQL Identifiers)

Automatically transforms snake_case database tables and column names into idiomatic Prisma PascalCase models and camelCase fields while inserting @@map and @map directives to maintain 100% database compatibility.

Default: camelCase & PascalCase with @map
Relation Generation ModeEnum (Bidirectional @relation | Scalar Foreign Keys Only)

Configures whether foreign key constraints generate bidirectional Prisma object relations with explicit fields and references arrays, or preserve raw scalar foreign key fields only.

Default: Bidirectional @relation
Enum StrategyEnum (Top-level Prisma Enum | String Scalar with Comment)

Determines whether PostgreSQL custom enum types and MySQL column-level ENUM declarations are extracted into top-level Prisma enum blocks or converted to standard String scalar types.

Default: Top-level Prisma Enum

Pro Tips & Best Practices

Preserve database naming conventions using @@map and @map

Enable camelCase and PascalCase mapping so your TypeScript Prisma client follows JavaScript naming conventions, while @@map('table_name') and @map('column_name') keep existing SQL database tables completely untouched without requiring database migrations.

Disambiguate multiple foreign keys referencing the same model

When a table has multiple foreign keys pointing to the same target table (such as author_id and reviewer_id referencing users), supply explicit relation names like @relation('PostAuthor', fields: [authorId], references: [id]) to prevent Prisma validation error P1012.

Map composite primary keys and multi-column unique constraints to @@id and @@unique

Ensure SQL composite primary keys like PRIMARY KEY (user_id, organization_id) are translated to @@id([userId, organizationId]), and multi-column indexes become @@index([fieldA, fieldB]) to reflect the exact query optimization topology.

Common Gotchas & Troubleshooting

Prisma Schema Validation Error P1012: 'The relation field cannot be parsed or lacks matching opposite field'

Foreign key relationships in Prisma require matching opposite relation fields on both referenced and referencing models. Ensure all related CREATE TABLE statements are included in the DDL input so the parser can synthesize bidirectional fields automatically.

Unsupported or dialect-specific column types (e.g., GEOMETRY, TSVECTOR, or custom GIS extensions)

Prisma provides the Unsupported('type_name') syntax for database-specific datatypes not natively mapped to standard Prisma scalars. Ensure unsupported fields are marked with Unsupported() or mapped to Bytes/String according to application requirements.

Frequently Asked Questions

How does the converter transform SQL foreign key constraints into Prisma relations?▼

When the parser encounters a FOREIGN KEY (author_id) REFERENCES users(id) clause, it generates both the scalar field (authorId Int @map('author_id')) and the virtual relation field (author User @relation(fields: [authorId], references: [id])), and automatically inserts a reverse relation field (posts Post[]) on the referenced User model.

What is the difference between this client-side converter and Prisma's native `prisma db pull` command?▼

`prisma db pull` requires an active network connection, direct database credentials, and introspects a live database instance. This client-side converter works 100% locally in your browser from raw SQL DDL migration files, without requiring database network access, credentials, or running database engines.

How are SQL composite primary keys and multi-column indexes represented in the generated Prisma schema?▼

Composite primary keys defined with PRIMARY KEY (col1, col2) are mapped to model-level @@id([col1, col2]) attributes. Composite unique constraints are mapped to @@unique([col1, col2]), and composite secondary indexes are mapped to @@index([col1, col2]).

Does the converter support SQL DEFAULT values and autoincrement sequences?▼

Yes. AUTO_INCREMENT (MySQL) and SERIAL / IDENTITY (PostgreSQL/SQL Server) clauses are converted to @default(autoincrement()), DEFAULT CURRENT_TIMESTAMP is converted to @default(now()), and custom default literals (integers, strings, booleans, and gen_random_uuid()) are mapped to corresponding Prisma @default(...) attributes.

Related Tools & Utilities

All Tools →