SQL to Drizzle Schema

Paste CREATE TABLE SQL and get a ready-to-use Drizzle ORM schema file. Choose the pg or mysql dialect and the converter emits the right table function, typed column helpers, .notNull(), .unique(), .primaryKey(), .references() for foreign keys, and the correct import line — no boilerplate hunting required.

How to use the SQL to Drizzle Schema

Paste one or more CREATE TABLE statements, pick a dialect (PostgreSQL or MySQL), and click Generate Schema. The output is a complete Drizzle schema file you can drop into your project.

  1. Dialect matters — PostgreSQL uses pgTable, serial, varchar, timestamp, and imports from drizzle-orm/pg-core. MySQL uses mysqlTable, int().autoincrement(), varchar, datetime, and imports from drizzle-orm/mysql-core.
  2. Foreign keys — REFERENCES clauses become .references(() => otherTable.column) inline on the column; the referenced table must be defined first in your schema or imported.
  3. Copy the output and save it as schema.ts (or schema.js). Run drizzle-kit generate to produce migrations from the new schema.

Note: the generated code uses named exports for each table (e.g. export const users = pgTable(...)). The import line at the top lists only the helpers actually used by your tables.

What is Drizzle ORM and why use a schema generator?

Drizzle ORM is a TypeScript-first relational query builder and ORM that keeps your database schema as the single source of truth. Unlike ActiveRecord-style ORMs that generate SQL from class definitions, Drizzle's schema files are the schema — typed column definitions that drive both compile-time type inference and the migration system (drizzle-kit). This means your TypeScript types and your database columns are always in sync without a separate type-generation step.

The schema file uses table helper functions (pgTable for PostgreSQL, mysqlTable for MySQL) and column helpers (serial, varchar, integer, boolean, timestamp, etc.) imported from the appropriate core package. Each column function is chainable: varchar('email', { length: 255 }).notNull().unique() reads almost like a column definition itself. Foreign key references use a callback .references(() => users.id) to avoid circular-import issues.

When you have an existing database — from a legacy app, a DBA hand-written migration, or a database design tool — you need to reverse-engineer the Drizzle schema before you can start using the ORM. Writing it by hand is tedious and error-prone. This converter does the mechanical translation, giving you a correct baseline that you can then customize (add indexes, rename columns, add relations objects) to suit your project.

Common use cases

  • Adopting Drizzle on a live database — generate the initial schema file from your existing DDL without writing every column by hand.
  • Switching from Prisma or TypeORM — export your schema as SQL DDL and convert it to the Drizzle equivalent in one step.
  • Multi-dialect projects — maintain one DDL source and generate both pg and mysql variants for staging vs. production.
  • Type-safe API layer — once the schema is in Drizzle, select queries return fully typed rows — no more any from raw SQL results.
  • Migration baseline — use the generated schema with drizzle-kit push to sync a fresh development database without writing explicit migrations.

Frequently asked questions

Does the output include Drizzle relations (one-to-many, many-to-many)?

No — the converter emits column-level .references() for foreign keys, which covers the structural FK constraint. Drizzle's relations() API is a separate layer for query-time joins and is project-specific. Add it manually after reviewing the generated schema.

What is the difference between pgTable and mysqlTable output?

For PostgreSQL: SERIAL becomes serial(), BOOLEAN becomes boolean(), TIMESTAMP becomes timestamp(). For MySQL: AUTO_INCREMENT becomes int().autoincrement(), TINYINT(1) becomes tinyint(), DATETIME becomes datetime(). The import path also changes from drizzle-orm/pg-core to drizzle-orm/mysql-core.

Can I use the generated schema with Drizzle Kit migrations?

Yes. Save the file as schema.ts, configure drizzle.config.ts to point at it, and run drizzle-kit generate to produce SQL migration files. Use drizzle-kit push for a quick schema sync in development.

What happens to composite primary keys?

Composite PKs from table-level PRIMARY KEY(a, b) are emitted as a comment — Drizzle handles them via the third argument of pgTable (a function returning { pk: primaryKey({ columns: [t.a, t.b] }) }). The converter notes this so you can add it manually.

Are indexes generated?

UNIQUE constraints on single columns become .unique() inline. For separate CREATE INDEX statements or multi-column indexes, Drizzle requires an index export in the table's third argument. The converter notes multi-column uniques as comments for you to implement.