drizzle-orm-schema-and-relations
Drizzle ORM Schema and Relations
Use this skill when the task is about schema declaration, relation modeling, or the ownership of database structure in Drizzle.
Scope
- dialect-specific table and column declaration
- schema organization across one or many files
- aliases, casing, and naming conventions
- indexes, constraints, views, sequences, and RLS
- soft relations and relational-query schema definitions
- beta-only relation-modeling changes like
defineRelations
Routing cues
pgTable,mysqlTable,sqliteTable, column types,pgSchema, constraints, views,relations,defineRelations, orcasing-> use this skill- query composition, CRUD, filters, joins, transactions, cache, or
sql-> usedrizzle-orm-queries-and-sql - database connection setup or runtime-specific drivers -> use
drizzle-orm-drivers-and-runtimes - migration generation, introspection, or
drizzle.config.ts-> usedrizzle-orm-migrations-and-drizzle-kit - Zod/Valibot/TypeBox schema generation, seed, GraphQL, or ESLint plugin -> use
drizzle-orm-ecosystem-and-extensions
Workflow
- Read references/schema-design.md first.
- If the task touches relations, advanced schema entities, or beta-only relation APIs, read references/relations-and-advanced-schema.md.
- Keep schema shape, relation definitions, and migration expectations consistent across the same dialect.
- Distinguish database-enforced foreign keys from Drizzle’s soft-relation/query helpers.
Quick example
import { integer, pgTable, text } from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm";
export const users = pgTable("users", {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
name: text().notNull(),
});
export const usersRelations = relations(users, ({ many }) => ({
posts: many(posts),
}));
Guardrails
- Always use the dialect-specific core module that matches the database you are targeting.
- If Drizzle Kit is part of the workflow, export every table, enum, view, sequence, and schema model that migrations should see.
- Choose either explicit aliases or DB-level
casingconventions deliberately; do not mix styles casually. - PostgreSQL schemas are first-class in Drizzle and Drizzle Kit. MySQL “schemas/databases” can be declared in Drizzle but are not managed by Drizzle Kit migrations.
- Treat
relations()/db.query.<table>anddefineRelations()/db._query.<table>as version-sensitive surfaces. The docs mark the newer relation flow as beta-line work. - Use soft relations for query ergonomics, but keep actual foreign key behavior represented in the schema when the database should enforce it.
Canonical APIs and concepts
pgTable,mysqlTable,sqliteTablepgSchema,mysqlSchemarelationsdefineRelationsindex,uniqueIndex,primaryKey- views, sequences, RLS, custom types
Maintenance
- Snapshot date: 2026-03-10
- Package snapshot:
drizzle-orm@0.45.1latest, beta tag1.0.0-beta.16-ea816b6
References
More from dobroslavradosavljevic/skills
base-ui-typescript-surface
Use for Base UI namespace contracts, change event types, generic render types, and wrapper typing patterns.
4base-ui-basic-primitives
Use for simple primitives with low integration complexity (Avatar, Button, Meter, Progress, Scroll Area, Separator).
4base-ui-root-providers
Use for CSP Provider, Direction Provider, and useDirection when wiring Base UI at the app root or across portals.
4base-ui-menus-navigation-and-toast
Use for Menu, Context Menu, Menubar, Navigation Menu, Toolbar, Toast, and shared command-surface patterns such as detached triggers and menu payloads.
4base-ui-forms-and-validation
Use for Base UI Field/Form primitives, validation flows, form controls, and advanced Number Field, Slider, and RadioGroup behavior.
4base-ui-disclosure-and-tabs
Use for stateful visibility components (Accordion, Collapsible) and tab navigation semantics.
4