Drizzle schema definition

From Intense-Visions/harness-engineering · View interactive version

Drizzle schema definition is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Define Drizzle ORM schemas with pgTable/mysqlTable/sqliteTable, column types, indexes, and constraints

How to use this skill

Skill file

Drizzle Schema Definition

Define Drizzle ORM schemas with pgTable/mysqlTable/sqliteTable, column types, indexes, and constraints

When to Use

Instructions

  1. Define a table using the provider-specific function. Export it as a const:
import { pgTable, text, timestamp, boolean, uuid } from 'drizzle-orm/pg-core';

export const users = pgTable('users', {
  id: uuid('id').primaryKey().defaultRandom(),
  email: text('email').notNull().unique(),
  name: text('name'),
  isActive: boolean('is_active').notNull().default(true),
  createdAt: timestamp('created_at').notNull().defaultNow(),
  updatedAt: timestamp('updated_at').notNull().defaultNow(),
});
  1. Choose the correct column types:
  1. Add constraints with chained methods:
export const posts = pgTable('posts', {
  id: uuid('id').primaryKey().defaultRandom(),
  slug: text('slug').notNull().unique(),
  title: text('title').notNull(),
  authorId: uuid('author_id')
    .notNull()
    .references(() => users.id),
  viewCount: integer('view_count').notNull().default(0),
});
  1. Add indexes as a third argument to the table function:
export const posts = pgTable(
  'posts',
  {
    // ... columns
  },
  (table) => [
    index('posts_author_idx').on(table.authorId),
    uniqueIndex('posts_slug_idx').on(table.slug),
    index('posts_author_created_idx').on(table.authorId, table.createdAt),
  ]
);
  1. Define enums (PostgreSQL only) separately and reference them:
import { pgEnum } from 'drizzle-orm/pg-core';

export const roleEnum = pgEnum('role', ['user', 'admin', 'moderator']);

export const users = pgTable('users', {
  role: roleEnum('role').notNull().default('user'),
});
  1. Composite primary keys:
import { primaryKey } from 'drizzle-orm/pg-core';

export const postTags = pgTable(
  'post_tags',
  {
    postId: uuid('post_id')
      .notNull()
      .references(() => posts.id),
    tagId: uuid('tag_id')
      .notNull()
      .references(() => tags.id),
  },
  (table) => [primaryKey({ columns: [table.postId, table.tagId] })]
);
  1. Infer TypeScript types from the schema:
import { InferSelectModel, InferInsertModel } from 'drizzle-orm';

export type User = InferSelectModel<typeof users>;
export type NewUser = InferInsertModel<typeof users>;
  1. Organize schemas by exporting all tables from a central db/schema.ts file that the Drizzle config references.

Details

Drizzle schemas are pure TypeScript — no code generation step, no DSL. The schema IS the TypeScript code, which means you get autocompletion, refactoring, and type inference for free.

Provider differences: pgTable (PostgreSQL), mysqlTable (MySQL), sqliteTable (SQLite) each support their native column types. You cannot mix providers in one schema. SQLite has the fewest column types; PostgreSQL has the most (arrays, jsonb, custom types).

Column naming convention: The first argument to each column function is the database column name. Use snake_case for database names while keeping TypeScript property names in camelCase:

// TypeScript: user.createdAt → Database: created_at
createdAt: timestamp('created_at').notNull().defaultNow(),

Default values: .default(value) sets a JavaScript default. .defaultNow() and .defaultRandom() generate SQL defaults (DEFAULT NOW(), DEFAULT gen_random_uuid()). Use SQL defaults when records may be inserted outside Drizzle.

References and foreign keys: .references(() => otherTable.column) creates a foreign key constraint. The arrow function is required to handle circular references between tables. Add { onDelete: 'cascade' } for cascade behavior.

Trade-offs:

Source

https://orm.drizzle.team/docs/column-types/pg

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.