defineRelations API (Drizzle 0.36+) in Drizzle ORM

From the Drizzle ORM cheat sheet · Relations · verified Jul 2026

defineRelations API (Drizzle 0.36+)

New unified relations builder — replaces per-table `relations()` calls

typescript
import { defineRelations } from 'drizzle-orm'
import * as schema from './schema'

export const relations = defineRelations(schema, (r) => ({
  users: {
    posts: r.many.posts(),
    profile: r.one.profiles({
      from: r.users.id,
      to: r.profiles.userId,
    }),
  },
  posts: {
    author: r.one.users({
      from: r.posts.authorId,
      to: r.users.id,
    }),
    tags: r.many.tags({
      from: r.posts.id.through(r.postTags.postId),
      to: r.tags.id.through(r.postTags.tagId),
    }),
  },
}))

// Query (the v2 relational API)
const db = drizzle(client, { schema, relations })
const user = await db.query.users.findFirst({
  where: { id: 1 },
  with: { posts: { with: { tags: true } } },
})
💡 v1 / RQB v2 API — install with npm i drizzle-orm@rc
📌 r.many / r.one with from/to type-checks against your schema
⚡ .through(joinTable.column) handles many-to-many without a separate model
🎯 Not in stable 0.45.x yet — classic relations() is today's default
relationsv2modern

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