HighTouch
HighTouch
DTDrizzle Team
Created by HighTouch on 6/19/2024 in #help
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 !
3 replies