Self-referencing schema failing to create first db item #23981
-
QuestionHello! Thank you for the library. 🙏 I'm learning on my feet, so please forgive any newb mistakes. The data I'm working with is very simple. The structure is a tree. There is a "root" element which has no parent, and the rest of the items reference another element as parent. For example, root element A has no parent. The next element is B and it refers to A as it's parent. I have attempted to model this in the schema (see below) by creating a self-referencing relation, and marking this field, I cannot proceed past creating the first item in the db. I have tried to create it in Prisma Studio, and there is some sort of bug rendering errors, that makes it quite difficult to debug. I have also turned on the debugger (output below). From what I can ascertain, it seems that prisma is failing because it is trying to reference another data point that does not yet exist. As I explained earlier, I have made that This is the failing code:
Here are the debug logs trying to save a new record in Prisma Studio:
How to reproduce (optional)Expected behavior (optional)Please help me figure out what I may have missed or do not understand about the self-referencing 1-M relation so that I can add that first data element, which does not refer to another element as it's parent. Information about Prisma Schema, Client Queries and Environment (optional)generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "cockroachdb"
url = env("DATABASE_URL")
}
model Leaf {
parentName String? @unique
name String @id @unique
description String
parent Leaf? @relation("LeafToLeaf", fields: [name], references: [parentName], onUpdate: Cascade, onDelete: Restrict)
children Leaf[] @relation("LeafToLeaf")
}
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I think I found the issue. I removed the
|
Beta Was this translation helpful? Give feedback.
I think I found the issue. I removed the
@unique
directive fromparentName
. Since a parent can have multiple children, this was invalid. The final schema, should anyone care to see it: