Self-Relation in Drizzle ORM?

I'm trying to create a folder/file structure in postgresql with drizzle orm I have the following table:
export const folder = pgTable("folders", {
id: uuid("id").primaryKey(),
name: varchar("name", {length: 64}).notNull(),
parent: uuid("parent").references(() => folder.id),
owner: uuid("owner").references(() => user.id),
color: varchar("color", {length: 7}).notNull(),
level: integer("level").notNull(),
createdAt: timestamp("createdAt").defaultNow().notNull(),
updatedAt: timestamp("updatedAt").defaultNow().notNull()
})
export const folder = pgTable("folders", {
id: uuid("id").primaryKey(),
name: varchar("name", {length: 64}).notNull(),
parent: uuid("parent").references(() => folder.id),
owner: uuid("owner").references(() => user.id),
color: varchar("color", {length: 7}).notNull(),
level: integer("level").notNull(),
createdAt: timestamp("createdAt").defaultNow().notNull(),
updatedAt: timestamp("updatedAt").defaultNow().notNull()
})
but im getting this error in typescript 'folder' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.ts(7022) Sorry if I overlooked something, I'm new to drizzle
No description
4 Replies
Sillvva
Sillvva9mo ago
https://orm.drizzle.team/docs/indexes-constraints#foreign-key
If you want to do a self reference, due to a TypeScript limitations you will have to either explicitly set return type for reference callback or user a standalone foreignKey operator.
import { serial, text, integer, foreignKey, pgTable, AnyPgColumn } from "drizzle-orm/pg-core";
export const user = pgTable("user", {
id: serial("id"),
name: text("name"),
parentId: integer("parent_id").references((): AnyPgColumn => user.id)
});
// or
export const user = pgTable("user", {
id: serial("id"),
name: text("name"),
parentId: integer("parent_id"),
}, (table) => {
return {
parentReference: foreignKey({
columns: [table.parentId],
foreignColumns: [table.id],
name: "custom_fk"
}),
};
});
import { serial, text, integer, foreignKey, pgTable, AnyPgColumn } from "drizzle-orm/pg-core";
export const user = pgTable("user", {
id: serial("id"),
name: text("name"),
parentId: integer("parent_id").references((): AnyPgColumn => user.id)
});
// or
export const user = pgTable("user", {
id: serial("id"),
name: text("name"),
parentId: integer("parent_id"),
}, (table) => {
return {
parentReference: foreignKey({
columns: [table.parentId],
foreignColumns: [table.id],
name: "custom_fk"
}),
};
});
Drizzle ORM - Indexes & Constraints
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
piton
pitonOP9mo ago
thanks a lot, searched on google for self reference in drizzle orm and this page didn't show up idk why
Sillvva
Sillvva9mo ago
Found it just by checking the section on foreign keys in indexes, but it's also showing up in the docs search.
No description
piton
pitonOP9mo ago
okay, thanks
Want results from more Discord servers?
Add your server