【问题标题】:How to upsert with arrays?如何使用数组进行更新插入?
【发布时间】:2021-05-23 00:25:15
【问题描述】:

也许这是一个非常愚蠢的问题,但是在更新插入时我应该如何将值推送到数组字段?我试图搜索它,但在他们的文档中找不到它。

这是我的架构:

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

这是我试图用来更新数组以向其推送项目的代码:

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'] },
      },
    });

我现在的使用方式是用type 数组替换['GRAYSCALE'] 数组。我想将 GRAYSCALE 项推送到数组中,而不是替换它。

我应该如何进行?

【问题讨论】:

    标签: node.js prisma


    【解决方案1】:

    目前无法直接upsert 一个数组。您需要先获取它,添加项目然后更新。

    我建议关注 this 请求并在问题中添加 ?,以便我们可以查看设置优先级。

    【讨论】:

    • 现在好像可以了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-06
    • 2012-10-04
    • 2014-01-15
    • 1970-01-01
    • 2014-05-03
    • 2021-12-14
    • 2012-06-16
    相关资源
    最近更新 更多