首页 > 解决方案 > Prisma - 在更多条件下更新一个资源

问题描述

我想知道是否可以在多种情况下更新资源。例如,考虑以下两个表:

+----------+----------+
|  Table1  |  Table2  |
+----------+----------+
| id       | id       |
| param1T1 | param1T2 |
| param2T1 | param2T2 |
| idTable2 |          |
+----------+----------+

我想将记录更新到表Table1中,我知道该字段idprimary keyAND,其中idTable2具有特定值。

使用该.update()方法我在编译时出错,但我不明白为什么我不能使用更多条件更新单个标识。这是我的更新:

table1.update({
  where: {
    AND: [
      { id: 1 },
      { Table2: { id: 1 } }
    ]
  },
  data: toUpdate
 });

现在,我.updateMany()改用.update(). 有解决方案还是不可能?

标签: node.jstypescriptprisma

解决方案


AND真的没有必要。根据您的架构,您可以尝试以下操作:

await prisma.table1.update({
        where: {  
            // the AND condition is implicit. Update only happens when both of the conditions provided hold true. 
            id: 1,
            idTable2: 1
        },
        data: toUpdate,
    });


推荐阅读