Jvwer
Jvwer
PPrisma
Created by Jvwer on 11/19/2024 in #help-and-questions
FindUnique/Update using unique index on sub field
Hi Raphael, Thanks for your fast reply. I've tinkered with this some more and found out it actually can be done. However the subfield should have a name that is not inside the model. so:
model Comment {
id String @id @default(auto()) @map("_id") @db.ObjectId
content String
replies Reply
...

@@unique([replies.replyId ], map: "replies_id_1")
@@map("comments")
}

type Reply {
replyId String @db.ObjectId @map("id")
content String
...
}
model Comment {
id String @id @default(auto()) @map("_id") @db.ObjectId
content String
replies Reply
...

@@unique([replies.replyId ], map: "replies_id_1")
@@map("comments")
}

type Reply {
replyId String @db.ObjectId @map("id")
content String
...
}
Now I can use it as
prisma.comment.findUnique({
where: { replyId: "someId" },
});
prisma.comment.findUnique({
where: { replyId: "someId" },
});
This is similar to how compound indexes work. Those have the same syntax: fieldName1_fieldName2. Because comment and reply both had id as a field, this wasn't possible. This was a little bit confusing 😅 but I got it working. Thanks!
4 replies