Drizzle ORM done right. Schema design, relations, type-safe queries, migrations, and performance patterns.
How this skill is triggered — by the user, by Claude, or both
Slash command
/drizzle-best-practices:drizzle-best-practicesThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Use this skill when working with Drizzle ORM code. Agents often confuse Drizzle with Prisma and
Use this skill when working with Drizzle ORM code. Agents often confuse Drizzle with Prisma and produce wrong schema syntax, incorrect relation patterns, or manual SQL where the query builder should be used.
Wrong (agents do this):
model User {
id Int @id @default(autoincrement())
name String
posts Post[]
}
Correct:
import { pgTable, serial, text } from 'drizzle-orm/pg-core';
export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
});
Why: Drizzle uses table-definition functions, not a Prisma-like DSL. Wrong syntax fails at compile time.
Wrong:
// Expecting Prisma-style @relation or Sequelize belongsTo
Correct:
import { relations } from 'drizzle-orm';
export const usersRelations = relations(users, ({ many }) => ({
posts: many(posts),
}));
export const postsRelations = relations(posts, ({ one }) => ({
author: one(users, { fields: [posts.authorId], references: [users.id] }),
}));
Why: Drizzle relations are defined separately from tables via relations(). Foreign keys go on
the table; relation metadata goes in relations.
Wrong:
const users = await db.execute(sql`SELECT * FROM users WHERE id = ${id}`);
Correct:
import { eq } from 'drizzle-orm';
const result = await db.select().from(users).where(eq(users.id, id));
Why: Query builder gives type safety, SQL injection protection, and dialect portability. Raw SQL only when the builder cannot express the query.
Wrong:
interface User {
id: number;
name: string;
}
Correct:
type SelectUser = typeof users.$inferSelect;
type InsertUser = typeof users.$inferInsert;
Why: Inferred types stay in sync with schema. Manual types drift and break on schema changes.
Wrong:
for (const id of ids) {
await db.select().from(users).where(eq(users.id, id));
}
Correct:
import { sql } from 'drizzle-orm';
const getById = db
.select()
.from(users)
.where(eq(users.id, sql.placeholder('id')))
.prepare();
for (const id of ids) {
await getById.execute({ id });
}
Why: Prepared statements reduce parse/plan overhead and improve performance for repeated queries.
Wrong:
await db.execute(sql`BEGIN`);
await db.insert(users).values(u);
await db.execute(sql`COMMIT`);
Correct:
await db.transaction(async (tx) => {
await tx.insert(users).values(u);
await tx.insert(posts).values(p);
});
Why: Drizzle transactions handle BEGIN/COMMIT/ROLLBACK and ensure the same connection is used throughout.
Wrong:
// Hand-written 001_create_users.sql
Correct:
drizzle-kit generate
drizzle-kit migrate
Why: Generated migrations stay in sync with schema and support rollback. Manual SQL bypasses Drizzle's migration tracking.
Wrong:
db.select().from(users).where(sql`name = ${name}`);
Correct:
import { eq, like, gt, lt } from 'drizzle-orm';
db.select().from(users).where(eq(users.name, name));
db.select().from(users).where(like(users.name, '%foo%'));
Why: Operators are type-safe and parameterized. Raw template strings risk SQL injection.
Wrong:
const users = await db.select().from(users);
for (const u of users) {
u.posts = await db.select().from(posts).where(eq(posts.authorId, u.id));
}
Correct:
const usersWithPosts = await db.query.users.findMany({
with: { posts: true },
});
Why: Relational API fetches nested data in one call with correct joins and typing.
Wrong:
// Separate migration: CREATE INDEX ...
Correct:
import { index } from 'drizzle-orm/pg-core';
export const users = pgTable('users', {
id: serial('id').primaryKey(),
email: text('email').notNull(),
}, (t) => [
index('users_email_idx').on(t.email),
]);
Why: Indexes in schema are versioned and migrated with the rest of the schema.
Wrong:
// Manually maintaining Zod schema that mirrors DB
Correct:
import { createInsertSchema } from 'drizzle-zod';
const insertUserSchema = createInsertSchema(usersTable);
Why: drizzle-zod derives Zod schemas from Drizzle tables, keeping validation in sync.
Wrong:
const db = drizzle(process.env.DATABASE_URL);
Correct:
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
const client = postgres(url, { max: 10, idle_timeout: 20 });
const db = drizzle(client);
Why: Serverless needs bounded connections. Default pooling can exhaust DB connections.
.returning() after insert/update to get affected rowsfindFirst over findMany when expecting a single row.$dynamic() for conditional where/orderBy/limitnpx claudepluginhub ofershap/drizzle-best-practicesExpert in Drizzle ORM for TypeScript — schema design, relational queries, migrations, and serverless database integration. Use when building type-safe database layers with Drizzle.
Provides Drizzle ORM patterns for schema definition, CRUD operations, relations, queries, transactions, and migrations. Supports PostgreSQL, MySQL, SQLite, MSSQL, CockroachDB.
Provides Drizzle ORM patterns for typesafe schema design, relational queries, prepared statements, migrations, and transactions.