Convert a Basic Prisma Model to a SQL Table

Learn how to convert a simple Prisma model definition into a SQL CREATE TABLE statement. Covers field-to-column mapping, type conversion, and the @id attribute.

Basic Models

Detailed Explanation

From Prisma Model to CREATE TABLE

The most fundamental conversion takes a Prisma model block and produces a SQL CREATE TABLE statement. Each field becomes a column with the appropriate SQL type, and attributes like @id are mapped to SQL constraints.

Example Prisma Schema

model User {
  id       Int     @id @default(autoincrement())
  name     String
  email    String  @unique
  age      Int?
  isActive Boolean @default(true)

  @@map("users")
}

Generated SQL (PostgreSQL)

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  email VARCHAR(255) NOT NULL UNIQUE,
  age INTEGER,
  is_active BOOLEAN NOT NULL DEFAULT TRUE
);

Key Mapping Rules

Prisma element SQL equivalent
model name Table name (snake_case or @@map value)
Field name Column name (snake_case or @map value)
Required field NOT NULL constraint
Optional field (?) Nullable column (no NOT NULL)
@id PRIMARY KEY
@default(...) DEFAULT ... clause
@unique UNIQUE constraint

Naming Conventions

Prisma convention uses PascalCase for model names and camelCase for field names. The converter translates these to snake_case for SQL table and column names. If a model has @@map("table_name"), that exact name is used instead. Similarly, @map("column_name") on a field overrides the automatic conversion.

Nullable vs Required Fields

In Prisma, fields are required by default. A ? suffix makes them optional. The converter adds NOT NULL to required fields and omits it for optional ones, which is the inverse of SQL's default behavior where columns are nullable unless constrained.

Use Case

You are bootstrapping a new project with Prisma and need to share the equivalent SQL schema with a DBA who reviews table structures in raw SQL before approving database changes.

Try It — Prisma to SQL Schema

Open full tool