Self-referencing many to many relation

I've been trying to point some interfaces to similar interfaces using the following code:
export const interfaces = createTable(
"interfaces",
{
id: varchar().notNull().primaryKey(),
name: varchar().notNull(),
type: interfacesTypesEnum().notNull(),
frequency: varchar().notNull(),
}
)

export const interfacesInterfaces = createTable(
"similar_interfaces",
{
interfaceId: varchar("interface_id")
.notNull()
.references(() => interfaces.id),
similarId: varchar("similar_id")
.notNull()
.references(() => interfaces.id),
},
(t) => ({
pk: primaryKey({ columns: [t.interfaceId, t.similarId] }),
})
);

export const interfacesInterfacesRelation = relations(interfacesInterfaces, ({ one }) => ({
asd: one(interfaces, {
fields: [interfacesInterfaces.interfaceId],
references: [interfaces.id],
relationName: "1"
}),
similar: one(interfaces, {
fields: [interfacesInterfaces.similarId],
references: [interfaces.id],
relationName: "2"
})
}))

export const interfaceInterfacesRelation = relations(interfaces, ({ many }) => ({
similarInterfaces: many(interfacesInterfaces, { relationName: "1" }),
}));
export const interfaces = createTable(
"interfaces",
{
id: varchar().notNull().primaryKey(),
name: varchar().notNull(),
type: interfacesTypesEnum().notNull(),
frequency: varchar().notNull(),
}
)

export const interfacesInterfaces = createTable(
"similar_interfaces",
{
interfaceId: varchar("interface_id")
.notNull()
.references(() => interfaces.id),
similarId: varchar("similar_id")
.notNull()
.references(() => interfaces.id),
},
(t) => ({
pk: primaryKey({ columns: [t.interfaceId, t.similarId] }),
})
);

export const interfacesInterfacesRelation = relations(interfacesInterfaces, ({ one }) => ({
asd: one(interfaces, {
fields: [interfacesInterfaces.interfaceId],
references: [interfaces.id],
relationName: "1"
}),
similar: one(interfaces, {
fields: [interfacesInterfaces.similarId],
references: [interfaces.id],
relationName: "2"
})
}))

export const interfaceInterfacesRelation = relations(interfaces, ({ many }) => ({
similarInterfaces: many(interfacesInterfaces, { relationName: "1" }),
}));
But I can't get the relation property to show up on the querying side
1 Reply
Angelelz
Angelelz2mo ago
You have to add a name to each relation so that drizzle can properly identify them I don't see the other side of "2"
Want results from more Discord servers?
Add your server