juan
DTDrizzle Team
•Created by juan on 11/5/2024 in #help
Self-referencing many to many relation
I've been trying to point some interfaces to similar interfaces using the following code:
But I can't get the relation property to show up on the querying side
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" }),
}));
3 replies