Become a Gold Sponsor

drizzle-kit generate

This guide assumes familiarity with:

drizzle-kit generate lets you generate SQL migrations based on you Drizzle schema upon declaration or on subsequent schema changes.

How it works under the hood?

Drizzle Kit generate command triggers a sequence of events:

  1. It will read through your Drizzle schema file(s) and compose a json snapshot of your schema
  2. It will read through your previous migrations folders and compare current json snapshot to the most recent one
  3. Based on json differences it will generate SQL migrations
  4. Save migration.sql and snapshot.json in migration folder under current timestamp
src/schema.ts
import * as p from "./drizzle-orm/pg-core";

export const users = p.pgTable("users", {
  id: p.serial().primaryKey(),
  name: p.text(),
  email: p.text().unique(), 
};
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                  
β”‚ $ drizzle-kit generate β”‚                  
β””β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                  
  β”‚                                           
  β”” 1. read previous migration folders
    2. find diff between current and previous scheama
    3. prompt developer for renames if necessary
  β”Œ 4. generate SQL migration and persist to file
  β”‚    β”Œβ”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  
  β”‚      πŸ“‚ drizzle       
  β”‚      β”œ πŸ“‚ _meta
  β”‚      β”” πŸ“œ 0000_premium_mister_fear.sql
  v
-- drizzle/0000_premium_mister_fear.sql

CREATE TABLE "users" (
 "id" SERIAL PRIMARY KEY,
 "name" TEXT,
 "email" TEXT UNIQUE
);

It’s designed to cover code first approach of managing Drizzle migrations. You can apply generated migrations using drizzle-kit migrate, using drizzle-orm’s migrate(), using external migration tools like bytebase or running migrations yourself directly on the database.

drizzle-kit generate command requires you to provide both dialect and schema path options, you can set them either via drizzle.config.ts config file or via CLI options

With config file
As CLI options
// drizzle.config.ts
import { defineConfig } from "drizzle-kit";

export default defineConfig({
  dialect: "postgresql",
  schema: "./src/schema.ts",
});
npx drizzle-kit generate

Schema files path

You can have a single schema.ts file or as many schema files as you want spread out across the project. Drizzle Kit requires you to specify path(s) to them as a glob via schema configuration option.

Example 1
Example 2
Example 3
Example 4
πŸ“¦ <project root>
 β”œ ...
 β”œ πŸ“‚ drizzle
 β”œ πŸ“‚ src
 β”‚ β”œ ...
 β”‚ β”œ πŸ“œ index.ts
 β”‚ β”” πŸ“œ schema.ts 
 β”œ πŸ“œ drizzle.config.ts
 β”” πŸ“œ package.json
import { defineConfig } from "drizzle-kit";

export default defineConfig({
  schema: "./src/schema.ts",
});

Custom migration file name

You can set custom migration file names by providing --name CLI option

npx drizzle-kit generate --name=init
πŸ“¦ <project root>
 β”œ πŸ“‚ drizzle
 β”‚ β”œ πŸ“‚ _meta
 β”‚ β”” πŸ“œ 0000_init.sql 
 β”œ πŸ“‚ src
 β”” …

Multiple configuration files in one project

You can have multiple config files in the project, it’s very useful when you have multiple database stages or multiple databases or different databases on the same project:

npm
yarn
pnpm
bun
npx drizzle-kit generate --config=drizzle-dev.config.ts
npx drizzle-kit generate --config=drizzle-prod.config.ts
πŸ“¦ <project root>
 β”œ πŸ“‚ drizzle
 β”œ πŸ“‚ src
 β”œ πŸ“œ .env
 β”œ πŸ“œ drizzle-dev.config.ts
 β”œ πŸ“œ drizzle-prod.config.ts
 β”œ πŸ“œ package.json
 β”” πŸ“œ tsconfig.json

Custom migrations

You can generate empty migration files to write your own custom SQL migrations for DDL alternations currently not supported by Drizzle Kit or data seeding. Extended docs on custom migrations - see here

drizzle-kit generate --custom --name=seed-users
πŸ“¦ <project root>
 β”œ πŸ“‚ drizzle
 β”‚ β”œ πŸ“‚ _meta
 β”‚ β”œ πŸ“œ 0000_init.sql 
 β”‚ β”” πŸ“œ 0001_seed-users.sql 
 β”œ πŸ“‚ src
 β”” …
-- ./drizzle/0001_seed-users.sql

INSERT INTO "users" ("name") VALUES('Dan');
INSERT INTO "users" ("name") VALUES('Andrew');
INSERT INTO "users" ("name") VALUES('Dandrew');

Extended list of available configurations

drizzle-kit generate has a list of cli-only options

customgenerate empty SQL for custom migration
namegenerate migration with custom name
npm
yarn
pnpm
bun
npx drizzle-kit push --name=init
npx drizzle-kit push --name=seed_users --custom



We recommend configuring drizzle-kit through drizzle.config.ts file, yet you can provide all configuration options through CLI if necessary, e.g. in CI/CD pipelines, etc.

dialectrequiredDatabase dialect, one of postgresql mysql sqlite turso
schemarequiredPath to typescript schema file(s) or folder(s) with multiple schema files
outMigrations output folder, default is ./drizzle
configConfiguration file path, default is drizzle.config.ts
breakpointsSQL statements breakpoints, default is true

Extended example

Example of how to create a custom postgresql migration file named 0001_seed-users.sql with Drizzle schema located in ./src/schema.ts and migrations folder named ./migrations instead of default ./drizzle.

We will also place drizzle config file in the configs folder.

Let’s create config file:

πŸ“¦ <project root>
 β”œ πŸ“‚ migrations
 β”œ πŸ“‚ configs
 β”‚ β”” πŸ“œ drizzle.config.ts
 β”œ πŸ“‚ src
 β”” …
drizzle.config.ts
import { defineConfig } from "drizzle-kit";

export default defineConfig({
  dialect: "postgresql",
  schema: "./src/schema.ts",
  out: "./migrations",
});

Now let’s run

npx drizzle-kit generate --config=./configs/drizzle.config.ts --name=seed-users --custom

And it will successfully generate

πŸ“¦ <project root>
 β”œ …
 β”œ πŸ“‚ migrations
 β”‚ β”œ πŸ“‚ _meta
 β”‚ β”œ πŸ“œ 0000_init.sql 
 β”‚ β”” πŸ“œ 0001_seed-users.sql 
 β”” …
-- ./drizzle/0001_seed-users.sql

INSERT INTO "users" ("name") VALUES('Dan');
INSERT INTO "users" ("name") VALUES('Andrew');
INSERT INTO "users" ("name") VALUES('Dandrew');