Convert a Simple SQL Table to a Prisma Model
Learn how to convert a basic SQL CREATE TABLE statement into a Prisma schema model. Covers column-to-field mapping, type inference, and the @id directive.
Detailed Explanation
From CREATE TABLE to Prisma Model
The most fundamental conversion takes a SQL CREATE TABLE statement and produces a Prisma model block. Each column becomes a field with a Prisma scalar type, and constraints like PRIMARY KEY are mapped to Prisma attributes.
Example SQL
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(320) NOT NULL,
age INTEGER,
is_active BOOLEAN NOT NULL DEFAULT true
);
Generated Prisma Schema
model User {
id Int @id
name String
email String
age Int?
isActive Boolean @default(true)
@@map("users")
}
Key Mapping Rules
| SQL element | Prisma equivalent |
|---|---|
| Table name | model name (PascalCase) + @@map("original_name") |
| Column name | Field name (camelCase) + @map("original_name") if different |
NOT NULL |
Field is required (default in Prisma) |
| Nullable column | Append ? to the type |
PRIMARY KEY |
@id attribute |
DEFAULT |
@default(...) attribute |
Table and Column Naming
Prisma conventions use PascalCase for model names and camelCase for field names. When the SQL table uses snake_case, the converter generates a @@map("table_name") directive on the model and @map("column_name") directives on fields whose names differ after camelCase conversion.
Nullable vs Required Fields
In SQL, columns are nullable by default unless marked NOT NULL. Prisma inverts this: fields are required by default. The converter marks nullable SQL columns with ? in Prisma and leaves NOT NULL columns as plain required fields.
Default Values
SQL DEFAULT clauses are translated to Prisma @default() attributes. Common defaults like true, false, numeric literals, and NOW() map directly. More complex SQL expressions may be preserved as @default(dbgenerated("...")).
This basic table-to-model conversion is the foundation for all other patterns. Once you understand this mapping, you can handle relations, indexes, enums, and advanced features.
Use Case
You have an existing SQL database with simple tables and want to start using Prisma ORM. Convert your existing DDL to a Prisma schema to bootstrap your project without manually writing every model definition.
Try It — SQL to Prisma Schema
Related Topics
Map SQL Primary Key Strategies to Prisma @id
Basic Models
Convert SQL String Columns to Prisma String Fields
Field Types
Map SQL Numeric Types to Prisma Int, Float, Decimal, and BigInt
Field Types
Map SQL DEFAULT Clauses to Prisma @default Attributes
Advanced Features
Convert SQL UNIQUE Constraints to Prisma @unique and @@unique
Advanced Features