Modelling self relations

I have a table categories with a parent fields:
{
id: text("id").notNull().primaryKey(),
....
parent: text("parent").references((): AnySQLiteColumn => categories.id)
}
{
id: text("id").notNull().primaryKey(),
....
parent: text("parent").references((): AnySQLiteColumn => categories.id)
}
Here one category can have one parent category. But, one category can have many subcategories. I have defined my relations:
export const categoryProductsRelation = relations(categories, ({ many, one })=>({
subcategories: many(categories, { relationName: "subcategories" })
}));

export const parentCategoryRelation = relations(categories, ({ many, one })=>({
parent: one(categories, {
fields: [categories.parent],
references: [categories.id]
})
}));
export const categoryProductsRelation = relations(categories, ({ many, one })=>({
subcategories: many(categories, { relationName: "subcategories" })
}));

export const parentCategoryRelation = relations(categories, ({ many, one })=>({
parent: one(categories, {
fields: [categories.parent],
references: [categories.id]
})
}));
But I am getting this error:
There is not enough information to infer relation \"categories.subcategories\"
There is not enough information to infer relation \"categories.subcategories\"
sps
sps322d ago
I want to retrieve a category along with its parent and sub-categories. Got it relationName is what I needed, it's not documented though
export const categoryProductsRelation = relations(categories, ({ many, one })=>({
productsOnCategory: many(products),
subcategories: many(categories, {relationName: "subcategories"}),
parentCategory: one(categories, {
fields: [categories.parent],
references: [categories.id],
relationName: "subcategories"
})
}));
export const categoryProductsRelation = relations(categories, ({ many, one })=>({
productsOnCategory: many(products),
subcategories: many(categories, {relationName: "subcategories"}),
parentCategory: one(categories, {
fields: [categories.parent],
references: [categories.id],
relationName: "subcategories"
})
}));
ReactPoriyaalar
ReactPoriyaalar191d ago
This should be documented
Angelelz
Angelelz191d ago
What do you think it's missing from the docs for this? Or what could be explained better? https://orm.drizzle.team/docs/rqb#declaring-relations
Drizzle Queries - DrizzleORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind
ReactPoriyaalar
ReactPoriyaalar191d ago
Unlike prisma docs , self relations are not documented I stand corrected. Just relaized what you shared is actually a self relation of users against users. May be the use case of having one to many self relations could be explained with an example . In general a dedicated section for self relations would clarify this ambiguity
Angelelz
Angelelz191d ago
We do have an open issue to add a "Recipes" section to the docs. We'll add examples there
Angelelz
Angelelz191d ago
Please add any idea you might have for examples to add to that https://github.com/drizzle-team/drizzle-orm/issues/235
GitHub
Add "Recipes" section to docs · Issue #235 · drizzle-team/drizzle-o...
Conditinally add query parts (join, limit, order by etc.) Conditional field selection Dynamic field selection (user-provided fields) Table self-reference Tables circular reference Using subqueries ...
Want results from more Discord servers?
Add your server
More Posts
Issue running migrations to DBI am having an issue running migrations to Neon. I checked that everything is exporting const variabIt is possible to have prepared statements inside transactions?Is there a way to insert prepared queries inside a transaction ?type config findMany or findFirstHi, I've one question i don't find the way to type correctly findMany() Like: export async functionAny idea on how to pass a pool from postgres-pool to drizzle?Passing a single client works, but for better connection management it would help to be able to passHow to get another linked tables count?I am new to Drizzle ORM and I wanted to get another tables data in my api. This is my api ```ts impI created a next auth adapter for postgres-jsThe open PR was not working for me so I created a new adapter. I added it to a starter project so pe.prepare() in Next.js App RouterHello! I'm trying to use PostgresQL prepared statement in the Next.js App Router and I have been getDeleting records w/ sqlite-core.Hello all, hoping to find some clarity/direction. So I'm trying to delete a record using; ```ts expCan't figure out how to design relational queryI have three tables - categories - products - media I want to retrieve products that belong to a padrizzle-zod@0.4.4 with drizzle-orm@0.26.5added drizzle-zod to my project and I am getting this error when starting the dev server - error ./ERR_MODULE_NOT_FOUND while generating migration files```ts import type { Config } from 'drizzle-kit'; export default { schema: './lib/db/schema/*', Typescript error with custom column typeThe select rules throws a TS error: "Expected 0 arguments, but got 1.ts(2554)" What am I doing wrongInfer TS types with `sql` operator and `db.execute````ts const db = drizzle(connection); export async function getTodos() { const result = await db.eMigrations not working with Neon.Tech?So i was trying to use drizzle for the first time. a little bit of a noob but im not sure what im doCannot read properties of undefined (reading 'connectionString')In my drizzle.config.ts I have the code: ``` import type { Config } from "drizzle-kit"; import 'doteNeed help with join, ChatGPT says drizzle orm does not support ithelp with drizzle types not inferred well``` async function addTopic(topic: string, content: string, parent?: string) { try { let parenInvalid input for drizzle-kit pg:inspectI'm running a command I've run a couple dozen times before, but now am getting an error saying I neeneed to change schema to reference another instanceInvalid input Only "mysql2" is available options for "--driver"I'm trying to run drizzle-kit introspect:mysql by following the docs and I have the config file: ``