drizzle-orm
Skill3 starsChanged 3 months agoMITView on GitHub
MUST USE when writing or reviewing Drizzle ORM schemas, migrations, relational queries, or drizzle-kit configuration. Enforces identity columns over serial, proper relation definitions, migration safety, type inference, and query patterns.
- Reads credentials
5 other projects use a version of this, 1 of them edited.
Adopted by
Most recently taken up by
- dallay/agents-skillstheir own version, first seen Aug 13, 2026
- suguru/agent-skillstheir own version, first seen May 12, 2026
- YepAPI/skillstheir own version, first seen Apr 12, 2026
- FelipeAngeli/skillstheir own version, first seen Apr 10, 2026
- Mindrally/skillsedited, first seen Jan 23, 2026
Other versions5 projects use a version of this.
Word for word · 0
None found.
Edited · 1
- Mindrally/skills★ 26610% same
Wrote their own version · 4
- YepAPI/skills★ 8same name
- dallay/agents-skills★ 2same name
- FelipeAngeli/skills★ 2same name
- suguru/agent-skills★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository16 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- code-structureSkill
- debuggingSkill
- docker-best-practicesSkill
- drf-best-practicesSkill
- expressSkill
- fastapiSkill
- git-merge-conflictsSkill
- reactSkill
- researchSkill
- security-practicesSkill
- sqlSkill
- tanstack-querySkill
- tanstack-routerSkill
- testing-best-practicesSkill
- typescript-best-practicesSkill
The file
We split this skill into the rules it gives an agent
1 rule. 0 are common, 0 are distinctive, and 1 is very specific to this project.
0
Common
Most of the 4 edited versions we read keep these.
0
Distinctive
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 328
Always review generated SQL: before running drizzle-kit migrate
“Common” is measured: kept by at least 6 in 10 of the 4 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Drizzle ORM Best Practices
Schema — Use Identity Columns, Not Serial
// BAD: serial is legacy PostgreSQL
import { pgTable, serial, text } from "drizzle-orm/pg-core";
export const users = pgTable("users", {
id: serial("id").primaryKey(),
name: text("name"),
});
// GOOD: identity columns are the modern PostgreSQL standard
import { pgTable, integer, text } from "drizzle-orm/pg-core";
export const users = pgTable("users", {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
name: text("name"),
});Schema — Column Naming
Map camelCase TypeScript to snake_case SQL explicitly.
// BAD: implicit column name matches TS key — inconsistent SQL
export const users = pgTable("users", {
firstName: varchar({ length: 256 }),
});
// GOOD: explicit snake_case SQL column name
export const users = pgTable("users", {
firstName: varchar("first_name", { length: 256 }),
});Schema — Indexes and Constraints
Define indexes in the third argument array:
export const posts = pgTable(
"posts",
{
id: integer().primaryKey().generatedAlwaysAsIdentity(),
slug: varchar({ length: 256 }),
title: varchar({ length: 256 }),
ownerId: integer("owner_id").references(() => users.id),
},
(table) => [
uniqueIndex("posts_slug_idx").on(table.slug),
index("posts_title_idx").on(table.title),
]
);Schema — Enums, Timestamps, Foreign Keys
// Define enums OUTSIDE the table
export const roleEnum = pgEnum("role", ["guest", "user", "admin"]);
export const posts = pgTable("posts", {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
title: text("title").notNull(),
role: roleEnum().default("guest"),
userId: integer("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at")
.notNull()
.$onUpdate(() => new Date()),
});Schema — Type Inference
// BAD: manually typing insert/select types
interface User { id: number; name: string; email: string; }
// GOOD: infer from schema — always in sync
export type InsertUser = typeof users.$inferInsert;
export type SelectUser = typeof users.$inferSelect;Relations — One-to-Many
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.userId],
references: [users.id],
}),
}));Relations — Many-to-Many
export const usersToGroups = pgTable("users_to_groups", {
userId: integer("user_id")
.notNull()
.references(() => users.id),
groupId: integer("group_id")
.notNull()
.references(() => groups.id),
}, (t) => [
primaryKey({ columns: [t.userId, t.groupId] }),
]);
export const usersRelations = relations(users, ({ many }) => ({
groups: many(usersToGroups),
}));
export const groupsRelations = relations(groups, ({ many }) => ({
members: many(usersToGroups),
}));Relational Queries — with and Filters
import * as schema from "./schema";
const db = drizzle(pool, { schema }); // pass schema to enable relational queries
// BAD: manual joins for simple relation fetching
const result = await db
.select()
.from(users)
.leftJoin(posts, eq(users.id, posts.userId));
// GOOD: relational query API — automatic joins, nested types
const result = await db.query.users.findMany({
with: {
posts: true,
},
});
// Filter and limit nested relations
const result = await db.query.users.findMany({
with: {
posts: {
where: (posts, { eq }) => eq(posts.published, true),
limit: 5,
orderBy: (posts, { desc }) => [desc(posts.createdAt)],
},
},
});Relations v2 — Landing in Drizzle v1.0
The relations API above is the stable 0.45.x syntax — keep using it until v1.0 ships. Drizzle v1.0 (currently drizzle-orm@rc) replaces per-table relations() with a single defineRelations, and you pass { relations } to drizzle() instead of { schema }. Know the shape so you recognize and can migrate v2 code:
import { defineRelations } from "drizzle-orm";
import * as schema from "./schema";
// v1 keys → v2 keys: fields → from, references → to, relationName → alias
export const relations = defineRelations(schema, (r) => ({
users: { posts: r.many.posts({ from: r.users.id, to: r.posts.authorId }) },
posts: { author: r.one.users({ from: r.posts.authorId, to: r.users.id }) },
}));
// many-to-many is native in v2 — no relation defined for the junction table:
// groups: r.many.groups({
// from: r.users.id.through(r.usersToGroups.userId),
// to: r.groups.id.through(r.usersToGroups.groupId),
// })
const db = drizzle(client, { relations }); // not { schema }v2 queries also take object-style where / orderBy and can filter parent rows by a related table's columns (v1 can only filter children):
await db.query.users.findMany({ where: { id: 1 }, with: { posts: true } });During the @rc migration window, v2 lives on db.query while your old v1-style callback queries keep working on db._query.
Queries — Select Only What You Need
// BAD: fetches all columns
const allUsers = await db.select().from(users);
// GOOD: partial select — less data over the wire
const names = await db
.select({ id: users.id, name: users.name })
.from(users);Queries — Insert, Update, Delete
// Insert with returning
const [newUser] = await db
.insert(users)
.values({ name: "Alice", email: "alice@example.com" })
.returning();
// Upsert (PostgreSQL)
await db
.insert(users)
.values({ email: "alice@example.com", name: "Alice" })
.onConflictDoUpdate({
target: users.email,
set: { name: "Alice Updated" },
});
// Update
await db.update(users).set({ name: "Bob" }).where(eq(users.id, 1));
// Delete
await db.delete(users).where(eq(users.id, 1));Transactions
// BAD: separate queries — no atomicity
await db.insert(orders).values(order);
await db.update(inventory).set({ stock: sql`stock - 1` }).where(eq(inventory.id, itemId));
// GOOD: wrap in transaction
await db.transaction(async (tx) => {
await tx.insert(orders).values(order);
await tx
.update(inventory)
.set({ stock: sql`stock - 1` })
.where(eq(inventory.id, itemId));
});Migrations — Config and Workflow
// drizzle.config.ts
import { defineConfig } from "drizzle-kit";
export default defineConfig({
dialect: "postgresql",
schema: "./src/db/schema.ts",
out: "./drizzle",
strict: true, // prompts on ambiguous changes like renames
dbCredentials: { url: process.env.DATABASE_URL! },
});# 1. Generate migration from schema diff
drizzle-kit generate --name=add_posts_table
# 2. Review the generated SQL in ./drizzle/ before applying
# 3a. Apply migrations (production — uses migration journal)
drizzle-kit migrate
# 3b. Push directly (dev only — no migration files)
drizzle-kit push
# Pull schema from existing database
drizzle-kit pull
# Custom/seed migration (empty SQL file you write yourself)
drizzle-kit generate --name=seed_users --customMigrations — Programmatic Apply
import { migrate } from "drizzle-orm/node-postgres/migrator";
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const db = drizzle(pool);
await migrate(db, { migrationsFolder: "./drizzle" });
await pool.end();Migrations — Rename Columns Safely
Drizzle Kit may interpret renames as drop + add = data loss. With strict: true it will prompt you. Write a custom migration instead:
-- drizzle/XXXX_rename_name_to_full_name/migration.sql
ALTER TABLE "users" RENAME COLUMN "name" TO "full_name";Migrations — Add Non-Nullable Column Safely
Generate the column addition, then use --custom for backfill + constraint:
ALTER TABLE "users" ADD COLUMN "role" VARCHAR(20); -- 1. nullable
UPDATE "users" SET "role" = 'member' WHERE "role" IS NULL; -- 2. backfill
ALTER TABLE "users" ALTER COLUMN "role" SET NOT NULL; -- 3. constrainRules
- Use identity columns (
generatedAlwaysAsIdentity()) overserialfor PostgreSQL - Explicit snake_case column names — always pass the SQL name string
- Infer types from schema — use
$inferInsert/$inferSelect, never manual interfaces - Define relations separately —
relations()calls live alongside table definitions - Use relational query API (
db.query.X.findMany({ with })) for nested data - Select only needed columns — avoid bare
select()in production queries - Wrap multi-table writes in transactions —
db.transaction() - Always review generated SQL before running
drizzle-kit migrate - Enable
strict: truein drizzle config — catches ambiguous renames - Never use
pushin production — always use migration files viagenerate+migrate - Three-step non-nullable columns — add nullable, backfill, set NOT NULL
- Commit migration files to version control — they are your database changelog
- Run
drizzle-kit generatein CI to detect schema drift
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

