xoldyckk
xoldyckk
Explore posts from servers
DTDrizzle Team
Created by xoldyckk on 8/14/2023 in #help
Need help implementing one to many relations for a table on itself
const comments = pgTable("comments", {
id: text("id").notNull().primaryKey(),

parentCommentId: text("parent_comment_id").references(
(): AnyPgColumn => comments.id
),
});

const commentsRelations = relations(comments, ({ many, one }) => ({
childComments: many(comments, {
relationName: "child_comments_from_parent_comment",
}),

parentComment: one(comments, {
fields: [comments.parentCommentId],
references: [comments.id],
relationName: "parent_comment_from_child_comment",
}),
}));
const comments = pgTable("comments", {
id: text("id").notNull().primaryKey(),

parentCommentId: text("parent_comment_id").references(
(): AnyPgColumn => comments.id
),
});

const commentsRelations = relations(comments, ({ many, one }) => ({
childComments: many(comments, {
relationName: "child_comments_from_parent_comment",
}),

parentComment: one(comments, {
fields: [comments.parentCommentId],
references: [comments.id],
relationName: "parent_comment_from_child_comment",
}),
}));
I want to implement a one to many self relation on the comments table, where a comment can have child comments recursively. The comment where parentCommentId is null would be the root comment that has no parent comment. Somehow this doesn't work. What's the problem in this schema? Here's the error:-
There is not enough information to infer relation "comments.childComments"
There is not enough information to infer relation "comments.childComments"
13 replies