'How to create an optional many:1 relationships from one table to itself in a Postgres database using Prisma?
I'm using Prisma with a Postgres database. I have the following model in my Prisma schema, the part of the model in question is the optional oldVersionId:
model Block {
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
actingUser User @relation(fields: [userId], references: [id])
userId String
accessType AccessType @default(PRIVATE)
blockType BlockType @default(IMPERATIVE)
blockState BlockState @default(NOT_STARTED)
humanText String
parents HierarchyRelation[] @relation("parents")
children HierarchyRelation[] @relation("children")
workspace String
workspaceOutgoing ReferenceRelation[] @relation("outgoing")
workspaceIncoming ReferenceRelation[] @relation("incoming")
oldVersion Block? @relation("lineage", fields: [oldVersionId], references: [id])
oldVersionId String?
newVersions Block[] @relation("lineage")
}
I want oldVersionId to be null in case the object is the original version. However, when I query the database with prisma.block.findMany(), I get the following error:
Invalid `prisma.block.findMany()` invocation: Attempted to serialize scalar 'null' with incompatible type 'String' for field oldVersionId.
Why is the null type not able to be serialized, and how else could I set up an optional many:1 relationship?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
