Handling self referencing foreign keys

I have the following code but am getting an error that says 'people' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.ts(7022) How should I handle a self referencing foreign key like in my example below?
export const people = pgTable("people",{
id: uuid("id").primaryKey(),
name: text("name").notNull(),
parentId: uuid("parent_id").notNull().references(() => people.id)
})
export const people = pgTable("people",{
id: uuid("id").primaryKey(),
name: text("name").notNull(),
parentId: uuid("parent_id").notNull().references(() => people.id)
})
2 Replies
Noahh
Noahh15mo ago
Does this work?
import {
AnyPgColumn
} from "drizzle-orm/pg-core";

export const people = pgTable("people",{
id: uuid("id").primaryKey(),
name: text("name").notNull(),
parentId: uuid("parent_id").notNull().references((): AnyPgColumn => people.id) // Added AnyPgColumn type here
})
import {
AnyPgColumn
} from "drizzle-orm/pg-core";

export const people = pgTable("people",{
id: uuid("id").primaryKey(),
name: text("name").notNull(),
parentId: uuid("parent_id").notNull().references((): AnyPgColumn => people.id) // Added AnyPgColumn type here
})
For reference, got this solution from https://orm.drizzle.team/docs/joins#aliases--selfjoins
Russell
RussellOP15mo ago
yup that works thank you!
Want results from more Discord servers?
Add your server