drizzle-orm-d1

Build type-safe schemas, queries, and migrations for Cloudflare D1 with Drizzle ORM.

3|1|Updated Aug 23, 2025
One-click install
npx skills add https://github.com/samuelpatro/.claude --skill drizzle-orm-d1-samuelpatro
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: drizzle-orm-d1
Source: https://github.com/samuelpatro/.claude/tree/main/skills/drizzle-orm-d1
Command: npx skills add https://github.com/samuelpatro/.claude --skill drizzle-orm-d1-samuelpatro

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires drizzle-orm, drizzle-kit, and includes references (resource) components.

What problem does it solve? Working with Cloudflare D1 through Drizzle ORM involves platform-specific pitfalls: D1 rejects SQL transactions, caps bound parameters at 100, silently drops relational query results when relations are not re-exported, and can destroy data during table-rebuild migrations. This Skill encodes the correct patterns and a catalog of real errors so you avoid these failures from the start. ## Core Features & Use Cases - Schema Design Patterns: Conventions for naming, primary keys, timestamps, indexes, enums, foreign keys with cascade rules, and relations that keep the relational query API working. - Query Recipes: CRUD operations, manual joins, the relational query API, db.batch for atomic multi-statement work, chunked inserts for the 100-parameter limit, prepared statements, and JSON column querying. - Migration Workflow: A safe generate-apply-deploy pipeline using wrangler with production env selection, rename handling, and guidance for dangerous table-rebuild migrations. - Error Catalog: Seventeen documented D1 + Drizzle errors with actual fixes, from BEGIN TRANSACTION failures to silent cascade data loss. - Use Case: You are adding a posts table with an author foreign key to a Cloudflare Worker. The Skill guides you to declare onDelete cascade, re-export postsRelations from the schema barrel, generate the migration with drizzle-kit, and apply it locally before remote. ## Quick Start Ask the AI to set up a Drizzle schema with users and posts tables for a Cloudflare D1 database, including the client factory and migration commands.

Frequently Asked Questions about drizzle-orm-d1

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I use Drizzle ORM with Cloudflare D1?▼

Install drizzle-orm, define tables with sqliteTable in a schema directory, and create a client with drizzle(d1, { schema }) imported from drizzle-orm/d1. Generate SQL with drizzle-kit generate and apply it with wrangler d1 migrations apply.

How do I run transactions in Cloudflare D1 with Drizzle?▼

D1 does not support SQL BEGIN or COMMIT, so db.transaction() throws a D1_ERROR. Use db.batch([...]) with an array of query builder statements instead, which executes atomically and returns results as a tuple.

Why is db.query.X undefined or missing relations in Drizzle?▼

The relational query API fails silently when the schema barrel does not re-export the *Relations objects or when drizzle() is called without the { schema } option. Re-export every table and its relations from schema/index.ts and pass the schema to the client factory.

Why does drizzle-kit generate produce a DROP COLUMN when I renamed a column?▼

Drizzle Kit cannot distinguish a rename from a drop plus add, so it prompts during generation. Select the rename option in the prompt to preserve data; accepting the default create option issues DROP COLUMN and ADD COLUMN, losing every row's value.

What is the bound parameter limit for D1 inserts?▼

D1 allows at most 100 bound parameters per statement, so a multi-row insert of N columns fits at most floor(100/N) rows. Chunk large inserts with a helper like insertInChunks instead of passing one big values array.

Why did my D1 migration fail or delete child rows on a table rebuild?▼

Drizzle-kit wraps rebuilds in PRAGMA foreign_keys=OFF, which is a no-op inside D1's migration transaction. The DROP TABLE either rolls back at COMMIT or cascades into child tables; fix it by parking child rows in save tables, rebuilding, then restoring.