P
Prisma•2w ago
monkeysjump

Using OR in connectOrCreate

I want to connect to an image if imageId is defined. If not, I want to connect to an image with a matching fileStorageObjectId. If it is not found I want to create a new image.
featuredImage: {
delete: true,
connectOrCreate: input.featuredImage
? {
where: {
OR: [
{ id: input.featuredImage.imageId },
{
fileStorageObjectId:
input.featuredImage.fileStorageObjectId,
},
],
},
create: input.featuredImage,
}
: undefined,
},
featuredImage: {
delete: true,
connectOrCreate: input.featuredImage
? {
where: {
OR: [
{ id: input.featuredImage.imageId },
{
fileStorageObjectId:
input.featuredImage.fileStorageObjectId,
},
],
},
create: input.featuredImage,
}
: undefined,
},
I get the error: Type '{ OR: ({ id: string | undefined; } | { fileStorageObjectId: string; })[]; }' is missing the following properties from type '{ fileStorageObjectId: string; id: string; entryId: string; }': fileStorageObjectId, id, entryId ts (2322) [511, 11]
2 Replies
Nurul
Nurul•2w ago
Hello @monkeysjump 👋 Prisma expects the where clause in connectOrCreate to contain unique fields that can identify a single record. Your current approach using OR in the where clause is not directly supported by Prisma's connectOrCreate operation. The where clause should typically contain a single unique identifier. To achieve what you're trying to do, you might need to split this operation into multiple queries 1. Try to find an existing image based on imageId or fileStorageObjectId 2. If found, connect to it. 3. If not found, create a new image.
monkeysjump
monkeysjumpOP•2w ago
Got it. Thank you!!
Want results from more Discord servers?
Add your server