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
Sillvva4mo 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
piton4mo ago
thanks a lot, searched on google for self reference in drizzle orm and this page didn't show up idk why
Sillvva
Sillvva4mo 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
piton4mo ago
okay, thanks
Want results from more Discord servers?
Add your server
More Posts
Drizzle spontaneously trying to truncate my dbToday I opened up Drizzle Studio, connected to my Vercel Postgres database, and a modal popped up sastring arrayHow are string arrays defined in drizzle? It always seems to revert back to json/jsonb when definingdates in sqlite not being evaluated, causing "Invalid Date"``` expiresAt: int("expires_at", { mode: "timestamp" }) .notNull() .$defaultFn(() =>drizzle-kit generate migration for pg is stuck at: Reading config file 'drizzle.config.ts'Hello, im using Deno 1.41.3. After executing `deno run -A npm:drizzle-kit generate:pg` cli just showHow do I reuse the results from the where clause within the findMany?I have a vendor ID and want to retrieve all restaurant IDs and menu IDs associated with that vendor.drizzle kit generate schemas for mysql had a little type mismatched problem.Has anyone encountered a similar problem of mismatched types with the error message "MySqlVarCharBuiHow to make a text based timestamp type for sqlite with zod schema inferenceWhen I use the built in integer with mode timestamp, the generated zod schemas correctly declare thaWhat's the best way to query my db?I'm running Supabase Postgres locally with Drizzle. In production, I'd use the connection pooler wHow to query many-many with mysqlI setup a simple example to test if many-many query and ran again into the same issue. ```ts Error:How do I access config in fromDriver and toDriver when defining a custom type using customType?None of the examples do this so it's not clear how/if this is possible?