How to upsert with arrays?

Viewed 1084

Maybe it's a really dumb question, but how should I push a value to an array field when upserting? I tried to search for it but couldn't find this in their documentation.

This is my schema:

model Chat {
  id                Int         @default(autoincrement()) @id
  type              Service[]
  chatId            String      @unique
}

And this is the code I'm trying to use to update the array to push an item to it:

const chatDb = await prisma.chat.upsert({
      where: {
        chatId: String(ctx.chat.id),
      },
      create: {
        chatId: String(ctx.chat.id),
        type: { set: ['GRAYSCALE'] },
      },
      update: {
        type: { set: ['GRAYSCALE'] },
      },
    });

How I am using right now, I'm substituting the type array for the ['GRAYSCALE'] array. I want to push the GRAYSCALE item to the array, and not substitute it.

How should I proceed?

1 Answers

Currently it's not possible to directly upsert an array. You would need to fetch it first, add the item and then update.

I would suggest following this request and adding a to the issue so that we can look set the priority.

Related