roninforge/roninforge-drizzle/drizzle-new-schema
Claude Code and agent skill by roninforge/roninforge-drizzle
- community
Checking that the SKILL.md file is still in the repository.
Scaffold a new Drizzle table: schema file with correct 3-arg pgTable shape, separate relations file, drizzle-zod validators, $inferSelect/$inferInsert types, idiomatic identity column + $onUpdate timestamp, optional index/unique-constraint declarations.
Install
npx vskill@latest install roninforge/roninforge-drizzle/drizzle-new-schemaSKILL.md
The same SKILL.md was published earlier as roninforge/roninforge-drizzle/drizzle-new-schema.
Scaffold Drizzle Schema
When to Use
When adding a new table to a Drizzle project, or when an LLM has generated something close-but-wrong (hand-written types, missing relations, wrong index shape).
Instructions
- Decide the table name and columns. For tables with >5 columns, give the table its own file under
src/db/schema/<name>.ts. For small lookup tables, group them in asrc/db/schema/lookup.ts.
- Write the schema file:
```typescript // src/db/schema/orders.ts import { pgTable, integer, varchar, timestamp, numeric, uniqueIndex, index, pgEnum, } from "drizzle-orm/pg-core"; import { sql } from "drizzle-orm"; import { users } from "./users";
export type OrderStatus = "pending" | "paid" | "shipped" | "cancelled";
export const orderStatusEnum = pgEnum("order_status", [ "pending", "paid", "shipped", "cancelled", ] as const);
export const orders = pgTable( "orders", { id: integer().primaryKey().generatedAlwaysAsIdentity(), userId: integer().notNull().references(() => users.id, { onDelete: "cascade" }), sku: varchar({ length: 64 }).notNull(), quantity: integer().notNull(), total: numeric({ precision: 10, scale: 2 }).notNull(), status: orderStatusEnum().notNull().default("pending").$type<OrderStatus>(), createdAt: timestamp({ withTimezone: true }).notNull().defaultNow(), updatedAt: timestamp({ withTimezone: true }).$onUpdate(() => new Date()), }, (t) => [ index("orders_user_id_idx").on(t.userId), uniqueIndex("orders_user_sku_idx").on(t.userId, t.sku), index("orders_status_idx").on(t.status), ], );
export type Order = typeof orders.$inferSelect; export type NewOrder = typeof orders.$inferInsert; ```
Rules:
- Identity column with
generatedAlwaysAsIdentity(), notserial(). withTimezone: trueon everytimestamp().$onUpdate(() => new Date())on theupdatedAtcolumn.- Third arg is an array of index/constraint expressions, not an object.
references()for foreign keys with explicitonDeletebehavior.$type<T>()to brand enum / union types.- Export
Order(select shape) andNewOrder(insert shape) at the bottom.
- Re-export from the schema index:
``typescript // src/db/schema/index.ts export * from "./users"; export * from "./orders"; ``
- Add relations to the relations file (NEVER inline in the schema file):
```typescript // src/db/relations.ts import { relations } from "drizzle-orm"; import { users } from "./schema/users"; import { orders } from "./schema/orders";
export const userRelations = relations(users, ({ many }) => ({ orders: many(orders), }));
export const orderRelations = relations(orders, ({ one }) => ({ user: one(users, { fields: [orders.userId], references: [users.id], }), })); ```
- Add a drizzle-zod validator next to the schema (optional but recommended):
```typescript // src/db/schema/orders.ts (append) import { z } from "zod"; import { createInsertSchema, createSelectSchema } from "drizzle-zod";
export const createOrderSchema = createInsertSchema(orders, { sku: (s) => s.sku.min(1).max(64), quantity: (s) => s.quantity.int().positive(), }).omit({ id: true, createdAt: true, updatedAt: true });
export type CreateOrderInput = z.infer<typeof createOrderSchema>; ```
- Generate a migration:
``bash pnpm drizzle-kit generate ``
Review the generated SQL in drizzle/000X_*.sql before committing. The generator can be confidently wrong about renames - confirm prompts when running interactively.
- Apply locally for development:
``bash pnpm drizzle-kit migrate ``
Or use drizzle-kit push for fast local iteration when you do not yet care about migration history.
Anti-patterns to avoid
- Never put
relations()in the same file as thepgTableit references. Circular imports follow. - Never use
serial()for new primary keys; identity columns are SQL-standard. - Never use
boolean()for status - usepgEnumso the constraint is enforced at the DB level. - Never declare
updatedAtwithout$onUpdate(...)- you will forget the manualset({ updatedAt })somewhere. - Never hand-write
interface Order { ... }types whentypeof orders.$inferSelectis one line. - Never mix
pgTablefrompg-corewithpgTablefrompostgres-js- the latter is a driver namespace, not a schema namespace.
What to ship
src/db/schema/<name>.ts(the table)- updated
src/db/schema/index.ts(re-export) - updated
src/db/relations.ts(relations between this table and others) drizzle/000X_<description>.sql(generated migration, committed)- updated
drizzle/meta/*.json(snapshot files, committed)
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/drizzle-new-schema/SKILL.md
Popularity
- Repository forks
- 1
- 7-day trend score
- 5
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/roninforge/roninforge-drizzle/drizzle-new-schema)