【问题标题】:Prisma - Update one resource under more conditionPrisma - 在更多条件下更新一个资源
【发布时间】:2021-11-16 21:28:46
【问题描述】:

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

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

我想将一条记录更新到表 Table1 中,我知道提交的 idprimary key 并且其中 idTable2 具有特定值。

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

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

现在,我使用 .updateMany() 而不是 .update() 来解决问题。有解决办法还是不可行?

【问题讨论】:

    标签: node.js typescript prisma


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-17
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      相关资源
      最近更新 更多