Indexes & Constraints in Drizzle ORM

From the Drizzle ORM cheat sheet · Schema Definition · verified Jul 2026

Indexes & Constraints

Add indexes, unique constraints, and composite keys to tables.

typescript
import { pgTable, serial, text, integer,
  timestamp, index, uniqueIndex } from 'drizzle-orm/pg-core';

export const posts = pgTable('posts', {
  id: serial('id').primaryKey(),
  title: text('title').notNull(),
  slug: text('slug').unique().notNull(),
  authorId: integer('author_id').notNull(),
  category: text('category'),
  createdAt: timestamp('created_at').defaultNow().notNull(),
}, (table) => ([
  index('author_idx').on(table.authorId),
  uniqueIndex('slug_idx').on(table.slug),
  index('category_author_idx')
    .on(table.category, table.authorId),
]));
💡 Third arg to pgTable defines indexes/constraints
⚡ Composite indexes speed up multi-column queries
📌 uniqueIndex() creates a UNIQUE index in the DB
🟢 Return an array of indexes from the callback
schemaindexesconstraints

Continue with Drizzle ORM

Save the full cheat sheet or work through every related task.

More Drizzle ORM tasks

Back to the full Drizzle ORM cheat sheet