Not enough information to infer relation with self-one-To-Many relation

Hello guys ! Thank for this community ! I'm looking everywhere to solve this. Got a schema with a question which can have parent question (one parentQuestion) and many child questions (subquestions) :
export const questionSchema = pgTable('questions', {
id: serial('id').primaryKey(),
text:...
parentId: integer('parentQuestion').references((): AnyPgColumn => questionSchema.id, { onDelete: 'cascade' }),
})

export const questionsRelations = relations(questionSchema, ({ many, one }) => ({
parentQuestion: one(questionSchema, {
fields: [questionSchema.parentQuestion],
references: [questionSchema.id],
relationName: 'parentQuestion',
}),
subQuestions: many(questionSchema, {
relationName: 'subQuestions',
}),
}))
export const questionSchema = pgTable('questions', {
id: serial('id').primaryKey(),
text:...
parentId: integer('parentQuestion').references((): AnyPgColumn => questionSchema.id, { onDelete: 'cascade' }),
})

export const questionsRelations = relations(questionSchema, ({ many, one }) => ({
parentQuestion: one(questionSchema, {
fields: [questionSchema.parentQuestion],
references: [questionSchema.id],
relationName: 'parentQuestion',
}),
subQuestions: many(questionSchema, {
relationName: 'subQuestions',
}),
}))
When i basically Query this :
const questions = await db.query.questionSchema.findMany({
with: {
subQuestions: true,
},
})
const questions = await db.query.questionSchema.findMany({
with: {
subQuestions: true,
},
})
I got Error: There is not enough information to infer relation "questionSchema.subQuestions" I can't manage to do it via query (it works with select but doesn't return me formatted data as i wish. Does anyone have insight about it ? Thank you !
2 Replies
HighTouch
HighTouchOP6mo ago
Sorry my research on the discord didn't show me this thread which explains what my mistake was: https://discord.com/channels/1043890932593987624/1173896750663213066/1173896750663213066 the mistake was that both relation names should be the same so in my schema i should have:
export const questionsRelations = relations(questionSchema, ({ many, one }) => ({
parentQuestion: one(questionSchema, {
fields: [questionSchema.parentQuestion],
references: [questionSchema.id],
relationName: 'parentQuestion',
}),
subQuestions: many(questionSchema, {
relationName: 'parentQuestion', // <========== notice parentQuestion instead of subquestions
}),
}))
export const questionsRelations = relations(questionSchema, ({ many, one }) => ({
parentQuestion: one(questionSchema, {
fields: [questionSchema.parentQuestion],
references: [questionSchema.id],
relationName: 'parentQuestion',
}),
subQuestions: many(questionSchema, {
relationName: 'parentQuestion', // <========== notice parentQuestion instead of subquestions
}),
}))
rubberduckies
rubberduckies6mo ago
i was also in need of this, thanks
Want results from more Discord servers?
Add your server