SQL to Drizzle Schema Converter

Convert SQL CREATE TABLE statements to Drizzle ORM TypeScript schema definitions for PostgreSQL, MySQL, and SQLite.

Imports:

What is SQL to Drizzle Schema Converter?

This tool converts SQL CREATE TABLE statements into Drizzle ORM TypeScript schema definitions. Drizzle ORM is a lightweight, type-safe ORM that provides SQL-like APIs for defining database schemas. The converter maps SQL data types to the appropriate Drizzle column builders for PostgreSQL, MySQL, or SQLite.

How to Use

  1. Select your database dialect (PostgreSQL, MySQL, or SQLite)
  2. Choose the import style: Table imports combines all imports, Inline separates them
  3. Paste your SQL CREATE TABLE statements into the input area
  4. Click "Generate Drizzle Schema" to create the TypeScript schema
  5. Copy the output and save it as a .ts file in your Drizzle ORM project

Why Use This Tool?

Column builders are dialect-specific: PostgreSQL gets serial() and jsonb(); MySQL gets mysqlEnum() and tinyint(); SQLite gets text() and blob()
REFERENCES foreign keys become .references(() => otherTable.column) — Drizzle's type-safe relational reference syntax
UNIQUE constraints become .unique() and composite unique constraints become uniqueIndex()
Generates the pgTable / mysqlTable / sqliteTable function call matching the dialect
No code generation required for the schema itself — the output is plain TypeScript that works immediately

Tips & Best Practices

  • Drizzle's query builder infers TypeScript types from the schema at compile time — use typeof usersTable.$inferSelect to get the TypeScript row type for a table without manually writing interfaces
  • For Cloudflare Workers or other edge environments, use drizzle-orm/neon-serverless (for Neon PostgreSQL) or drizzle-orm/libsql (for Turso/libsql) — these drivers work in edge runtimes without Node.js dependencies
  • Drizzle Kit's push command (drizzle-kit push) applies schema changes directly to the database without generating migration files — useful for development. Use generate + migrate for production to maintain an auditable migration history
  • Unlike Prisma, Drizzle does not require explicit relation() blocks to query with joins — you can join any tables using db.select().from().leftJoin(). The relations() function is optional and only needed for the Drizzle Relational Queries API
  • JSONB columns in PostgreSQL become jsonb(). Use .$type<YourType>() to add a TypeScript type annotation: jsonb("metadata").$type<Record<string, unknown>>()

Frequently Asked Questions

What is the complete SQL type → Drizzle column builder mapping?

PostgreSQL: SERIAL → serial(), VARCHAR(n) → varchar({length:n}), TEXT → text(), INTEGER → integer(), BIGINT → bigint({mode:"number"}), BOOLEAN → boolean(), TIMESTAMP → timestamp(), DATE → date(), JSONB → jsonb(), UUID → uuid(), DECIMAL(p,s) → decimal({precision:p,scale:s}). MySQL: VARCHAR → varchar({length:n}), INT → int(), BIGINT → bigint({mode:"number"}), TINYINT(1) → boolean(), DATETIME → datetime(), JSON → json(). SQLite: TEXT → text(), INTEGER → integer(), REAL → real(), BLOB → blob().

How does Drizzle differ from Prisma for schema definition?

Prisma uses its own .prisma schema language parsed by a separate binary. Drizzle uses TypeScript — the schema IS the TypeScript code, no transformation step needed. Prisma generates a client (npx prisma generate) that is separate from your schema. Drizzle's client is initialized directly from your schema. Prisma has more built-in tooling (Studio, introspect). Drizzle is lighter (smaller bundle, edge-compatible) and gives you more SQL control. Neither is universally better; choice depends on project requirements.

When should I NOT use Drizzle?

Skip Drizzle when: you need a visual database admin interface (Prisma Studio is more polished); you are building a non-TypeScript Node.js project (Drizzle is TypeScript-first); or you need deep ORM features like lifecycle hooks, complex virtual fields, or middleware on queries (Prisma has more of these).

Is my SQL data sent to a server?

No. All conversion runs entirely in your browser. Your SQL schema never leaves your device.

Real-world Examples

Blog platform: posts table with author foreign key

A typical blog posts table with an author foreign key, slug unique constraint, and nullable published_at timestamp. Drizzle generates typed column builders with .references() for the foreign key.

Input
CREATE TABLE posts (
  id SERIAL PRIMARY KEY,
  title VARCHAR(255) NOT NULL,
  slug VARCHAR(255) NOT NULL UNIQUE,
  content TEXT,
  author_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  is_published BOOLEAN NOT NULL DEFAULT false,
  view_count INTEGER NOT NULL DEFAULT 0,
  published_at TIMESTAMP,
  created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
Output
import { pgTable, serial, varchar, text, integer, boolean, timestamp } from 'drizzle-orm/pg-core';
import { usersTable } from './users';

export const postsTable = pgTable('posts', {
  id: serial('id').primaryKey(),
  title: varchar('title', { length: 255 }).notNull(),
  slug: varchar('slug', { length: 255 }).notNull().unique(),
  content: text('content'),
  authorId: integer('author_id').notNull().references(() => usersTable.id, { onDelete: 'cascade' }),
  isPublished: boolean('is_published').notNull().default(false),
  viewCount: integer('view_count').notNull().default(0),
  publishedAt: timestamp('published_at'),
  createdAt: timestamp('created_at').notNull().defaultNow(),
});

// TypeScript types:
// type Post = typeof postsTable.$inferSelect;
// type NewPost = typeof postsTable.$inferInsert;

Related Tools