One-to-Many Relation in Drizzle ORM
From the Drizzle ORM cheat sheet · Relations · verified Jul 2026
One-to-Many Relation
Define a one-to-many relationship between users and posts.
typescript
import { relations } from 'drizzle-orm';
import { pgTable, serial, text,
integer } 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').notNull(),
});
export const usersRelations = relations(users, ({ many }) => ({
posts: many(posts),
}));
export const postsRelations = relations(posts, ({ one }) => ({
author: one(users, {
fields: [posts.authorId],
references: [users.id],
}),
}));💡 Relations are ORM-level, not DB constraints
⚡ relations() is the stable API (drizzle-orm@latest)
📌 fields = FK column, references = target PK column
🟢 Pass schema to drizzle() to use relations
relationsone-to-many