relations to the same table

Is this not possible to do?
export const CompaniesRelations = relations(Companies, ({ many, one }) => ({
clients: many(Companies),
parentCompany: one(Companies, {
fields: [Companies.parentCompanyId],
references: [Companies.id],
}),
websites: many(Websites),
}));
export const CompaniesRelations = relations(Companies, ({ many, one }) => ({
clients: many(Companies),
parentCompany: one(Companies, {
fields: [Companies.parentCompanyId],
references: [Companies.id],
}),
websites: many(Websites),
}));
I get an error when rinning the studio: Error: There are multiple relations between "__public__.Companies" and "companies". Please specify relation name this error disappears if i remove the self referential relations
8 Replies
⚡Z.E.U.S⚡
⚡Z.E.U.S⚡8mo ago
Hey! Is this relationship described in your schema?
rubberduckies
rubberduckiesOP8mo ago
hey. no, it's not, but that has never been a problem
⚡Z.E.U.S⚡
⚡Z.E.U.S⚡8mo ago
Where did you get this relation from then?
rubberduckies
rubberduckiesOP8mo ago
it just "is"
⚡Z.E.U.S⚡
⚡Z.E.U.S⚡8mo ago
You need to specify relationName when you define multiple relations between the same two tables https://orm.drizzle.team/docs/rqb#disambiguating-relations
Drizzle ORM - Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
rubberduckies
rubberduckiesOP8mo ago
No description
⚡Z.E.U.S⚡
⚡Z.E.U.S⚡8mo ago
export const CompaniesRelations = relations(Companies, ({ many, one }) => ({
clients: many(Companies, { relationName: 'clients' }),
parentCompany: one(Companies, {
fields: [Companies.parentCompanyId],
references: [Companies.id],
relationName: 'parentCompany'
}),
websites: many(Websites),
}));
export const CompaniesRelations = relations(Companies, ({ many, one }) => ({
clients: many(Companies, { relationName: 'clients' }),
parentCompany: one(Companies, {
fields: [Companies.parentCompanyId],
references: [Companies.id],
relationName: 'parentCompany'
}),
websites: many(Websites),
}));
rubberduckies
rubberduckiesOP8mo ago
thank you
Want results from more Discord servers?
Add your server