繁体   English   中英

Prisma:在单个查询中更新嵌套实体

[英]Prisma: update nested entities in a single query

我正在使用 Prisma 和 Express.js 向我的 MySQL 数据库表发出请求。

我的Contest-Round表之间存在一对多关系,我正在尝试编写一个查询,以允许我以不同方式更新给定比赛的回合。 给定架构:

model Contest {
    id            Int      @id @default(autoincrement())
    name          String   @unique
    rounds        Round[]
    ... other fields
}

model Round {
    id        Int       @id @default(autoincrement())
    name      String
    contestId Int
    contest   Contest   @relation(fields: [contestId], references: [id], onDelete: Cascade)
    ... other fields
}

我想要实现的是从更新比赛

{
    id: 100,
    name: 'contest1',
    rounds: [
        {
             id: 1,
             name: 'round1',
             contestId: 100,
        },
        {
             id: 2,
             name: 'round2',
             contestId: 100,
        }
    ]

例如

{
    id: 100,
    name: 'contest1',
    rounds: [
        {
             id: 1,
             name: 'round1Updated',
             contestId: 100,
        },
        {
             id: 2,
             name: 'round2UpdatedDifferently',
             contestId: 100,
        }
    ]

我从 HTML 的表单值中获得的回合名称。

我还没有找到任何关于更新不同嵌套实体的例子,所以我希望它是这样的:

     var updated = await prisma.contest.update({
            where: {
                id: 100
            },
            data: {
                name: data.name,
                rounds: {
                    update: {
                        where: {
                            id: { in: [1, 2] },
                        },
                        data: {
                            name: ['round1Updated', 'round2UpdatedDifferently']   
                        },
                    },
                }
            },
            include: {
                rounds: true,
            }
        });

任何想法或线索将不胜感激。

所以最初的要求有点改变,我只需要根据表单数据添加/删除回合(而不是更新为问题中的 state)。 我最终首先获得了比赛,然后是应该添加/删除的回合:

// get the target contest first
contes contest = await prisma.contest.findFirst({
  where: { id },
  select: {
    rounds: true,
  }
});

// newly created round on my form
const roundsToBeCreated = // rounds that the existing contest doesn't have yet, but the form does

// deleted rounds on my form
const roundsToBeDeleted = // rounds that the existing contest has, but form data doesn't

const updated = await prisma.contest.update({ where: { id },
        data: {
            rounds: {
                deleteMany: {
                    id: {
                        in: roundsToBeDeleted.map(r => r.id)
                    },
                },
                createMany: {
                    data: roundsToBeCreated
                },
            }
        }
    });

对于更改Rounds的字段,我认为此查询不适合,因为我还有其他依赖于Rounds的实体。 并且仅更改例如回合名称我将完全丢失他们的记录。 因此,为此我认为我需要一个单独的查询来更新Round条目。

这样的事情会起作用吗?

const updated = await prisma.contest.update({
  where: { id: 100 },
  data: {
    name: data.name,
    rounds: {
      deleteMany: { id: { in: [1, 2] } }, // Delete existing records first
      createMany: { // Update by creating new records
        data: [
          { id: 1, name: "round1Updated" },
          { id: 2, name: "round2UpdatedDifferently" },
        ]
      }
    }
  },
  include: { rounds: true }
});

从表单数据

const updated = await prisma.contest.update({
  where: { id: data.id },
  data: {
    name: data.name,
    rounds: {
      deleteMany: { id: { in: data.rounds.map(({ id }) => id) } },
      createMany: { data: rounds }
    }
  },
  include: { rounds: true }
});

请注意,操作的顺序很重要:
https://github.com/prisma/prisma/discussions/6263

const updated = await prisma.contest.update({
  where: {id: data.id},
  data: {
    name: data.name,
    rounds: {
      deleteMany: {},
      createMany: {data: rounds},
    }
  },
  include: {rounds: true},
});

暂无
暂无

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

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