Enforce uniqueness on Json fields

How can I enforce uniqueness on the post model based on the values in the json fields. here is the schema:
model Post {
id String @id @default(cuid())
title String
content String
collectionId String
collection Collection @relation(fields: [collectionId], references: [id])
metadata PostMetadata? @relation(name: "PostMetadata")
extraData Json? @db.JsonB
tags Tag[] @relation(name: "PostTags")
}
model Post {
id String @id @default(cuid())
title String
content String
collectionId String
collection Collection @relation(fields: [collectionId], references: [id])
metadata PostMetadata? @relation(name: "PostMetadata")
extraData Json? @db.JsonB
tags Tag[] @relation(name: "PostTags")
}
the extraData fields json will look like:
{
surah: string:
verse: number
}
{
surah: string:
verse: number
}
I don't want two records with same values to exist, like if there is record with these in the json field surah: "24", verse: 30, I don't want another to be created with same values
1 Reply
RaphaelEtim
RaphaelEtim2w ago
Hi @Anas Badran Currently, Prisma does not support JSON schema validation or constraints on specific JSON keys, you might need to implement this validation at the application level. You would need to check for existing records with the same surah and verse values before creating or updating a post. We do have an open issue to add support for Json schema validation. You can leave a 👍 and add your usecase to help our engineers prioritize it.
GitHub
Support for JSON Schema Validation in Prisma Models for Relationnal...
Problem Currently, when working with JSON fields in Prisma with a relationnal database, it is not possible to automatically generate a schema or enforce structure within these fields. This limitati...
Want results from more Discord servers?
Add your server