What is the best way to handle nested one to many ...
# prisma-client
l
What is the best way to handle nested one to many self relations? I tried:
Copy code
model Item {
  id             String   @id @default(uuid())
  relatedItemsId String[]
  relatedItems   Item[]   @relation("RelatedItems", fields: [relatedItemsId], references: [id])
}
But it throwing and error in my schema file.
1
r
Hi @Larry Mickie 👋, Please could you share more information about the errors you are experiencing with regards the nested one to many self relations. Also you can take a look at our documentation on how to properly compose a one to many self relation. Modifying your code slightly, this should work
Copy code
model Item {
id String @id @default(uuid())
relatedItemsId String[]
relatedItems Item[] @relation("RelatedItems")
Item Item? @relation("RelatedItems", fields: [relatedItemsId], references: [id])
}
1
l
Hey Raphael! Thanks alot. Question - what would the "Item" parameter be a reference to?
r
Hi Larry 👋 The
relatedItems
and
Item
fields will not be present in the database. The Item table will only have
id
and
relatedItemsId
columns. The relation columns only exists at prisma level and not at database level. So the relation
Item
field is referencing the
id
column which is the primary key for this table.
👍 1