© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
2 replies
son dang

Self referencing

I have schema:
export const categories = pgTable('categories',{
    id: integer('id').primaryKey(),
    name: varchar('name',{length: 255}),
    parent: integer('parent').references((): AnyPgColumn => categories.id)
});

export const categoriesOneRelations = relations(categories, ({ one }) => ({
    parent: one(categories, { fields: [categories.parent], references: [categories.id] }),
}));

export const categoriesManyRelations2 = relations(categories, ({ many }) => ({
    children: many(categories),
}));
export const categories = pgTable('categories',{
    id: integer('id').primaryKey(),
    name: varchar('name',{length: 255}),
    parent: integer('parent').references((): AnyPgColumn => categories.id)
});

export const categoriesOneRelations = relations(categories, ({ one }) => ({
    parent: one(categories, { fields: [categories.parent], references: [categories.id] }),
}));

export const categoriesManyRelations2 = relations(categories, ({ many }) => ({
    children: many(categories),
}));

Code:
const categories = await db.query.categories.findMany({
        with: {
            children: true
        },
    });

console.log('categories:', JSON.stringify(categories, null, 2));
const categories = await db.query.categories.findMany({
        with: {
            children: true
        },
    });

console.log('categories:', JSON.stringify(categories, null, 2));

after execute then throw exeception
drizzle-orm/alias-72a4082c.cjs:3541
            : new Error(`There are multiple relations between "${referencedTableTsName}" and "${relation.sourceTable[Table.Symbol.Name]}". Please specify relation name`);
              ^

Error: There are multiple relations between "categories" and "categories". Please specify relation name
drizzle-orm/alias-72a4082c.cjs:3541
            : new Error(`There are multiple relations between "${referencedTableTsName}" and "${relation.sourceTable[Table.Symbol.Name]}". Please specify relation name`);
              ^

Error: There are multiple relations between "categories" and "categories". Please specify relation name

Please help me!
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

SQLite self referencing table
Drizzle TeamDTDrizzle Team / help
12mo ago
Self referencing table query
Drizzle TeamDTDrizzle Team / help
2y ago
Handling self referencing foreign keys
Drizzle TeamDTDrizzle Team / help
3y ago
drizzle-kit pull & self-referencing relations
Drizzle TeamDTDrizzle Team / help
2mo ago