Need help implementing one to many relations for a table on itself
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:-
5 Replies
Won't you need to add the fields and references for this? Like you've got in parentComment?
As it will be looking for a comment_id (Or something similar) most-likely in the query to join on.
in a one to many relation no
since the parent can never know how many children it has
a child has exactly one parent referenced by the parentCommentId field present on it
querying for the child comments is an implementation detail abstracted away from us i believe
only the
relationName
field is available when defining relations with many
keyword
if it's not abstracted away then we probably have to do it manually when using query client, then perhaps the only use of defining relations table is for typescript intellisenseYes I get you, although to resolve the error you've posted I think it needs the additional info so that
comments.childComments
will work 🙂 I'm not 100% sure though as I've not got something to test it on right now.Okay some stuff happened, idk what but it suddenly started
Haha, no worries! Glad it's working! 😄