Skip to content
SyntaxSnap

SQL to Drizzle ORM Schema

Paste CREATE TABLE statements from PostgreSQL, MySQL or SQLite and get a Drizzle schema file: typed columns, defaults, foreign keys, composite primary keys and optional relations.

SQL

Drizzle schema (PostgreSQL)

import { boolean, integer, jsonb, pgTable, primaryKey, serial, text, timestamp, uuid, varchar } from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  publicId: uuid('public_id').notNull().defaultRandom(),
  email: varchar('email', { length: 255 }).notNull().unique(),
  name: text('name'),
  isActive: boolean('is_active').notNull().default(true),
  createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
});

export const posts = pgTable('posts', {
  id: serial('id').primaryKey(),
  authorId: integer('author_id').notNull().references(() => users.id, { onDelete: 'cascade' }),
  title: varchar('title', { length: 200 }).notNull(),
  status: text('status').notNull().default('draft'),
  meta: jsonb('meta'),
  publishedAt: timestamp('published_at'),
});

export const postTags = pgTable('post_tags', {
  postId: integer('post_id').notNull().references(() => posts.id),
  tag: text('tag').notNull(),
}, (t) => [
  primaryKey({ columns: [t.postId, t.tag] }),
]);

export const usersRelations = relations(users, ({ many }) => ({
  posts: many(posts),
}));

export const postsRelations = relations(posts, ({ one, many }) => ({
  author: one(users, { fields: [posts.authorId], references: [users.id] }),
  postTags: many(postTags),
}));

export const postTagsRelations = relations(postTags, ({ one }) => ({
  post: one(posts, { fields: [postTags.postId], references: [posts.id] }),
}));
3 tables

How to convert SQL to a Drizzle schema

  1. Step 1

    Paste your CREATE TABLE statements

    Paste one or more statements. The database is detected from the syntax, or you can pick it.

  2. Step 2

    Review the notes

    Read any notes about unmapped types or references to tables that are missing from the input.

  3. Step 3

    Copy schema.ts

    Copy the schema or download it as schema.ts and add it to your Drizzle config.

Moving from Prisma or raw SQL to Drizzle

Drizzle schemas are plain TypeScript, so the quickest way to migrate an existing database is to start from its DDL. Export it with pg_dump --schema-only, mysqldump --no-data or .schema in SQLite, paste the CREATE TABLE statements here and save the result as schema.ts. If you are coming from Prisma, SQL to Prisma does the reverse trip.

How columns are mapped

Each column becomes the matching builder from drizzle-orm/pg-core, mysql-core or sqlite-core, keyed in camelCase with the original column name kept as the database name. SERIAL becomes serial(), AUTO_INCREMENT becomes .autoincrement(), now() and CURRENT_TIMESTAMP become .defaultNow() where the column type supports it, and gen_random_uuid() becomes .defaultRandom(). Numeric and decimal defaults are written as strings, because Drizzle returns those columns as strings to avoid precision loss.

What to check by hand

Indexes, CHECK constraints, triggers, views and generated columns are not converted. Types without a built-in Drizzle builder, such as PostgreSQL bytea, are mapped to text and listed in the notes under the output so you can swap in a customType. Relations are inferred from single-column foreign keys; rename them if the generated names do not read well.

Questions and answers

Is my database schema uploaded?

No. Parsing and code generation run in your browser, so table and column names never leave your device.

Which Drizzle version is the output for?

It uses the current API, where extra table config such as composite keys is returned as an array from the third argument of pgTable, mysqlTable or sqliteTable.

Does it generate drizzle-kit migrations?

No. It writes the schema file. Run drizzle-kit pull against a live database if you want Drizzle to introspect it, or drizzle-kit generate after adding this schema to create migrations.

Are indexes converted?

Not yet. Add them in the table's third argument with index().on(...) after converting.

Can I convert a Prisma schema to Drizzle?

Not directly. Generate SQL from Prisma with prisma migrate diff, then paste that SQL here.

Next step

Need this built into your stack?

The team behind SyntaxSnap builds custom code generators, internal developer tools and API integrations.