roninforge/roninforge-drizzle/drizzle-new-schema

Claude Code and agent skill by roninforge/roninforge-drizzle

Trust tier 3
  • community
Source check pending

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-schema

SKILL.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

  1. 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 a src/db/schema/lookup.ts.
  1. 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(), not serial().
  • withTimezone: true on every timestamp().
  • $onUpdate(() => new Date()) on the updatedAt column.
  • Third arg is an array of index/constraint expressions, not an object.
  • references() for foreign keys with explicit onDelete behavior.
  • $type<T>() to brand enum / union types.
  • Export Order (select shape) and NewOrder (insert shape) at the bottom.
  1. Re-export from the schema index:

``typescript // src/db/schema/index.ts export * from "./users"; export * from "./orders"; ``

  1. 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], }), })); ```

  1. 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>; ```

  1. 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.

  1. 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 the pgTable it references. Circular imports follow.
  • Never use serial() for new primary keys; identity columns are SQL-standard.
  • Never use boolean() for status - use pgEnum so the constraint is enforced at the DB level.
  • Never declare updatedAt without $onUpdate(...) - you will forget the manual set({ updatedAt }) somewhere.
  • Never hand-write interface Order { ... } types when typeof orders.$inferSelect is one line.
  • Never mix pgTable from pg-core with pgTable from postgres-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

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
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/roninforge/roninforge-drizzle/drizzle-new-schema/badge)](https://verified-skill.com/skills/roninforge/roninforge-drizzle/drizzle-new-schema)

Something wrong with this skill or its page? Tell us.