one-to-many self-relation
Hello, I'm trying to create a one-to-many self-relation but I get the error
There is not enough information to infer relation "comment.replies"
, i looked at the previous threads but it didn't seem to solve my problem
export const comment = pgTable("comment", {
id: uuid("id").notNull().unique().primaryKey().defaultRandom(),
parentId: uuid("parent_id"),
comment: text("comment").notNull(),
userId: uuid("user_id").references(() => users.id, { onDelete: "cascade" }).notNull(),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
deleted: boolean("deleted").default(false),
})
export const commentRelations = relations(comment, (({ one, many }) => ({
user: one(users, {
fields: [comment.userId],
references: [users.id]
}),
replies: many(comment, { relationName: "replies" }),
})))
4 Replies
Do you have the inverse of the relation in the schema as well?
So something like
parentComment: one(comment, {
fields: [comment.parentId],
references: [comment.id],
relationName: "replies"
})
@Brooks693 thank you, this solution helped me too!
Thank you very much, it solved my problem
@Brooks693 @Mykhailo @Syuro
Hello there, I got the same problem and this solution fixed it, umm partially.
Selecting comments with parentComment works. but selecting comment with replies doesn't.
I my case, it's about categories.
this is the categories schema:
Now this query works:
But this gives me an error:
And the error is:
Is this even possible? or only joining with parentId would work?
Sorry to bother, Found the solution.
parent and children need to have the same relation name