简体   繁体   English

Prisma 更新与 WHERE 属性中的关系

[英]Prisma update with relation in WHERE property

Given these schemas:鉴于这些模式:

model awayinfo {
PlayerID   Int        @id
IsAway     Boolean
playerinfo playerinfo @relation(fields: [PlayerID], references: [ID])
}
    
model playerinfo {
name       String    @db.VarChar(15)
ID         Int       @id @unique @default(autoincrement())
awayinfo   awayinfo?
}

How would I create a prisma SQL update for the awayinfo table if the only identifier I have would be the Name of a player and not the ID?如果我拥有的唯一标识符是玩家的姓名而不是 ID,我将如何为 awayinfo 表创建 prisma SQL 更新?

what I tried:我尝试了什么:

I try to pass something into the WHERE part as seen below:我尝试将一些东西传递到 WHERE 部分,如下所示:

const result = await prisma.awayinfo.update({
    where: {
      PlayerID: {
        name: name
      }
  },
    data: {
      IsAway: true,
    }
  });

but it always gives me the Error:但它总是给我错误:

Invalid `prisma.awayinfo.update()` invocation: 

Argument PlayerID: Got invalid value
{
  name: 'Dummy'

}
on prisma.updateOneawayinfo. Provided Json, expected Int.

I got pretty desperate and even tried wonky selects like this one我非常绝望,甚至尝试过像这样的不稳定选择

 const result = await prisma.awayinfo.update({
    where: {
      PlayerID: {
      playerinfo: {
        Where: {
        name: name
      },
      select: {ID: true},
    }}
  }, .....

but obviously this would not work aswell.但显然这也行不通。 I wonder what I am missing here and I cannot find any example of a condition within the WHERE clause in the prisma documentation我想知道我在这里遗漏了什么,我在 prisma 文档的 WHERE 子句中找不到任何条件示例

There are two issues here, the first is in your Prisma schema/data model and the second is in your query.这里有两个问题,第一个在您的 Prisma 模式/数据 model 中,第二个在您的查询中。

Making name uniquename独一无二

Firstly, if you want to uniquely identify a record in the playerinfo table with just the name property, this property must be unique .首先,如果你想name属性唯一标识playerinfo表中的一条记录,这个属性必须unique Otherwise, it's not possible for your database (and Prisma) to know which player info record to update in case there are multiple records with the same name property.否则,如果有多个具有相同name属性的记录,您的数据库(和 Prisma)不可能知道要更新哪个玩家信息记录。

Update your schema accordingly:相应地更新您的架构:

model playerinfo {
  name     String    @unique @db.VarChar(15)
  ID       Int       @id @unique @default(autoincrement())
  awayinfo awayinfo?
}

Rewriting the update query重写更新查询

Since the where condition in your update references a property playerinfo model, that is where you should begin your query.由于更新中的where条件引用了属性playerinfo model,因此您应该从那里开始查询。 Here's what it looks like:这是它的样子:

const data = await prisma.playerinfo.update({
    where: {
        name: name
    }, 
    data: {
        awayinfo: {
            update: {
                IsAway: true
            }
        }
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM