简体   繁体   English

在一个查询中更新多个表

[英]Update multiple tables in one query

I couldn't find any relevant info on Prisma docs, nor on SO on this question.我在 Prisma 文档上找不到任何相关信息,也没有在这个问题上找到任何相关信息。

My relevant schema looks like this:我的相关架构如下所示:

model User {
  id             Int       @default(autoincrement()) @id
  createdAt      DateTime  @default(now())
  updatedAt      DateTime  @updatedAt
  firstName      String?
  lastName       String?
  email          String    @unique
  hashedPassword String?
  role           String    @default("user")
  sessions       Session[]
  profile        Profile?
}

model Profile {
  id                 Int       @default(autoincrement()) @id
  aboutMe            String?
  location           String?
  profession         String?
  user               User      @relation(fields:[userId], references: [id])
  userId             Int
}

I want to update multiple columns in both tables however, have been unable to get the mutation to work in Prisma.但是,我想更新两个表中的多个列,但无法在 Prisma 中使突变生效。 So, this is what my mutation looks like at the moment:所以,这就是我的突变现在的样子:

 ...
  const {aboutMe, location, profession, firstName, lastName } = inputs;
  const profile = await db.user.update({
    where: { id: ctx.session!.userId },
    data: {
      profile: {
        update: {
          aboutMe,
          location,
          profession,
        },
      },
    },
  });
  const user = await db.user.update({
    where: { id: ctx.session!.userId },
    data: {
      firstName,
      lastName,
    },
  });
...

As you can see, I have two mutations, is it possible the update multiple tables with a single mutation?如您所见,我有两个突变,是否可以使用单个突变更新多个表?

Thank you.谢谢你。

This should work:这应该有效:

const profile = await db.user.update({
    where: { id: u.id },
    data: {
      firstName: 'name',
      lastName: 'name',
      profile: {
        update: {
          aboutMe: 'aboutMe',
          location: 'aboutMe',
          profession: 'aboutMe',
        },
      },
    },
    include: { profile: true },
  })

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

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