DT
Drizzle Team•10mo ago
laf_RSW

One-to-Many Self relation, Not enough information to infer relation

Im currently working on a piece of software for infrastructure management. This software contains so-called "controls" that need to be done periodically. Every control can be followed up by multiple controls (called follow up control). But each control can only have one parent control it follows up on. I have created the following schema:
// controls Table
export const controls = pgTable("controls", {
id: serial("id").primaryKey().notNull(),
passed: boolean("passed").notNull().default(false),
user_id: bigint("user_id", { mode: 'number' }).references(() => users.id, {onUpdate: 'cascade', onDelete: 'restrict'}).notNull(),
followUpForControl_id: bigint("followUpForControl_id", { mode: 'number' }).references((): AnyPgColumn => controls.id, {onUpdate: 'cascade', onDelete: 'restrict'}),
});

export const controlRelations = relations(controls, ({ one, many }) => ({
user: one(users, {
fields: [controls.user_id],
references: [users.id]
}),
followUpControls: many(controls, {
relationName: 'followUpControls'
}),
followUpForControl: one(controls, {
fields: [controls.followUpForControl_id],
references: [controls.id],
relationName: 'followUpForControl'
}),
}));
// controls Table
export const controls = pgTable("controls", {
id: serial("id").primaryKey().notNull(),
passed: boolean("passed").notNull().default(false),
user_id: bigint("user_id", { mode: 'number' }).references(() => users.id, {onUpdate: 'cascade', onDelete: 'restrict'}).notNull(),
followUpForControl_id: bigint("followUpForControl_id", { mode: 'number' }).references((): AnyPgColumn => controls.id, {onUpdate: 'cascade', onDelete: 'restrict'}),
});

export const controlRelations = relations(controls, ({ one, many }) => ({
user: one(users, {
fields: [controls.user_id],
references: [users.id]
}),
followUpControls: many(controls, {
relationName: 'followUpControls'
}),
followUpForControl: one(controls, {
fields: [controls.followUpForControl_id],
references: [controls.id],
relationName: 'followUpForControl'
}),
}));
However when i try to query with followUpControls i get "There is not enough information to infer relation "controls.followUpControls""
4 Replies
laf_RSW
laf_RSWOP•10mo ago
I saw that post but im not sure how that applies here. Since each control can have multiple follow up controls but only one control it follows up on, wouldn't that make it a one-to-many relation and not many-to-many as stated in the post you linked?
Angelelz
Angelelz•10mo ago
Ok, I just looked at your code. I think you might only need to give then the same relationName
laf_RSW
laf_RSWOP•10mo ago
That did the trick, don't know why i didn't think of that myself. Thanks a bunch 🙂
Want results from more Discord servers?
Add your server