🔧 Error Fixes
· 1 min read

Drizzle ORM: Relation Not Found — How to Fix It


Error: Relation not found

Drizzle can’t find the relation you defined.

Fix 1: Define relations correctly

import { relations } from 'drizzle-orm';

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

export const posts = pgTable('posts', {
  id: serial('id').primaryKey(),
  authorId: integer('author_id').references(() => users.id),
});

// ✅ Define relations separately
export const usersRelations = relations(users, ({ many }) => ({
  posts: many(posts),
}));

export const postsRelations = relations(posts, ({ one }) => ({
  author: one(users, { fields: [posts.authorId], references: [users.id] }),
}));

Fix 2: Pass relations to drizzle()

import * as schema from './schema';
const db = drizzle(client, { schema });