SQL to Prisma Schema with Relations

Convert SQL CREATE TABLE statements to Prisma schema with automatic relation generation. Supports foreign keys, one-to-many, and one-to-one relationships.

What is SQL to Prisma Schema with Relations?

Prisma is a modern database toolkit that provides a declarative schema language, type-safe query client, and migration system for PostgreSQL, MySQL, SQLite, SQL Server, and MongoDB. Unlike traditional ORMs that use decorators or configuration files, Prisma uses a single schema.prisma file as the source of truth for your data model. This converter analyzes SQL CREATE TABLE statements and generates Prisma schema models with proper scalar type mappings, @id and @default annotations, and bidirectional @relation declarations derived from foreign key references. Unique foreign keys produce one-to-one relations (Profile?), while regular foreign keys produce one-to-many relations (Post[]).

How to Use

  1. Paste one or more SQL CREATE TABLE statements that include REFERENCES constraints for foreign key relationships
  2. Click Generate to produce a Prisma schema with models, attributes, and bidirectional relation fields
  3. Review the generated relations: parent models get array fields (children[]) and child models get scalar fields plus relation references
  4. Copy the output into your schema.prisma file and add the datasource and generator blocks at the top
  5. Run npx prisma generate to create the Prisma Client, then npx prisma db push to sync the schema with your database

Why Use This Tool?

Automatically generates bidirectional Prisma relations from unidirectional SQL foreign keys, saving you from manually computing reverse relation fields
Unique foreign key constraints are detected and produce one-to-one relations with the ? modifier, while non-unique foreign keys produce one-to-many array relations
SERIAL and BIGSERIAL types are mapped to Int @id @default(autoincrement()) and BigInt @id @default(autoincrement()) respectively
DEFAULT values like NOW() are converted to Prisma @default(now()) and boolean defaults map to @default(true) or @default(false)
The generated schema is ready for Prisma Migrate — run npx prisma migrate dev to create database migrations from the schema

Tips & Best Practices

  • Prisma requires a datasource block (specifying your database provider and URL) and a generator block at the top of schema.prisma — add these manually after pasting the generated models
  • For composite primary keys, you will need to add @@id([field1, field2]) manually since SQL single-column PRIMARY KEY constraints are the only ones auto-detected
  • Add @@index([]) annotations for columns frequently used in WHERE clauses or JOIN conditions to improve query performance
  • When a foreign key column has a UNIQUE constraint, the converter correctly generates a one-to-one relation — the referenced model gets an optional singular field rather than an array

Frequently Asked Questions

How are SQL types mapped to Prisma scalar types?

The converter follows Prisma conventions: INTEGER, INT, and SERIAL map to Int; BIGINT and BIGSERIAL map to BigInt; VARCHAR, CHAR, TEXT, and UUID map to String; BOOLEAN maps to Boolean; TIMESTAMP, TIMESTAMPTZ, DATE, and DATETIME map to DateTime; DECIMAL and NUMERIC map to Float (use Decimal from prisma-client for exact precision); JSON and JSONB map to Json; and BLOB maps to Bytes.

When should I not use Prisma for my project?

Prisma may not be the best fit if you need raw SQL performance for complex analytical queries, database-specific features like PostgreSQL window functions, or if your team prefers a code-first approach over schema-first. Also, Prisma does not support many-to-many relations without an explicit join table model, and its migration system can be limiting for highly customized database schemas.

Is my SQL data sent to a server?

No. All parsing and code generation runs entirely in your browser. Your SQL schema never leaves your device, and no network requests are made during the conversion process.

How does the converter detect one-to-one vs one-to-many relations?

When a foreign key column has a UNIQUE constraint alongside REFERENCES, the converter generates a one-to-one relation. The referenced model gets an optional singular field (e.g., profile Profile?), while without UNIQUE it gets an array field (e.g., posts Post[]). This matches Prisma semantics where uniqueness on the foreign key side means at most one child can reference each parent.

Can I use the generated schema with existing databases?

Yes. Use npx prisma db pull to introspect an existing database and compare it with the generated schema, or use npx prisma db push to apply the schema without creating migration files. For production databases, always use npx prisma migrate dev to create reviewed migration SQL before applying changes.

Real-world Examples

Social media schema with users, posts, and profiles

A social platform needs Prisma models where users have many posts and optionally one profile, with the one-to-one profile relation detected from a UNIQUE foreign key.

Input
CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  email TEXT UNIQUE NOT NULL
);
CREATE TABLE profiles (
  id SERIAL PRIMARY KEY,
  user_id INTEGER UNIQUE NOT NULL REFERENCES users(id),
  bio TEXT
);
Output
model Users {
  id      Int      @id @default(autoincrement())
  email   String   @unique
  profile Profiles?
  posts   Posts[]
}

model Profiles {
  id     Int    @id @default(autoincrement())
  userId Int    @unique
  bio    String?
  user   Users  @relation(fields: [userId], references: [id])
}

E-commerce order system with customer-order relationships

An e-commerce application needs Prisma models where customers have many orders, and each order references its customer through a foreign key.

Input
CREATE TABLE customers (
  id SERIAL PRIMARY KEY,
  name VARCHAR(100) NOT NULL
);
CREATE TABLE orders (
  id SERIAL PRIMARY KEY,
  customer_id INTEGER NOT NULL REFERENCES customers(id),
  total DECIMAL(10,2) NOT NULL
);
Output
model Customers {
  id     Int      @id @default(autoincrement())
  name   String
  orders Orders[]
}

model Orders {
  id         Int      @id @default(autoincrement())
  customerId Int
  total      Float
  customer   Customers @relation(fields: [customerId], references: [id])
}

Related Tools