Foreign Keys in Drizzle ORM

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

Foreign Keys

Define foreign key references between tables.

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

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  name: text('name').notNull(),
});

export const posts = pgTable('posts', {
  id: serial('id').primaryKey(),
  title: text('title').notNull(),
  authorId: integer('author_id')
    .references(() => users.id, {
      onDelete: 'cascade',
    })
    .notNull(),
  createdAt: timestamp('created_at').defaultNow().notNull(),
});
💡 .references() creates a DB-level foreign key
⚡ onDelete: 'cascade' auto-deletes child rows
📌 Options: cascade, restrict, no action, set null
🟢 FK is separate from Drizzle relations (ORM)
schemaforeign-keyreferences

More Drizzle ORM tasks

Back to the full Drizzle ORM cheat sheet