son dang
son dang
DTDrizzle Team
Created by son dang on 7/19/2023 in #help
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!
3 replies